44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { ErrorsConfig } from '@config/config';
|
|
|
|
import { SettingsCache } from '@seed/graphql/Settings';
|
|
import { ApolloError } from 'apollo-server-lambda';
|
|
import { UtilStringHelper } from './Utils.string';
|
|
|
|
type ErrorKeys = keyof typeof ErrorsConfig;
|
|
|
|
const utilStringHelper = new UtilStringHelper();
|
|
|
|
export const newError = (code: ErrorKeys, data?: any): ApolloError => {
|
|
const errorConfig = SettingsCache.getInstance().cache.errorsContent;
|
|
|
|
const message = errorConfig[code].message;
|
|
const settings = errorConfig[code].translations;
|
|
|
|
const translations = {
|
|
subject: {},
|
|
body: {},
|
|
};
|
|
|
|
if (settings) {
|
|
if (settings.body) {
|
|
for (const key in settings.body) {
|
|
if (Object.prototype.hasOwnProperty.call(settings.body, key)) {
|
|
translations.body[key] = utilStringHelper.interpolate(settings.body[key], data);
|
|
}
|
|
}
|
|
}
|
|
if (settings.subject) {
|
|
for (const key in settings.subject) {
|
|
if (Object.prototype.hasOwnProperty.call(settings.subject, key)) {
|
|
translations.subject[key] = utilStringHelper.interpolate(settings.subject[key], data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new ApolloError(utilStringHelper.interpolate(message, data), code.toString(), {
|
|
translations,
|
|
data,
|
|
});
|
|
};
|