Skip to content

Commit

Permalink
Use default message when i18n is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
knor-el-snor committed Apr 27, 2018
1 parent 6a7e58e commit a6dddf2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"express-validation": "~1.0.2",
"http-status": "~1.0.1",
"i18n": "~0.8.3",
"joi": "~13.1.2",
"joi": "~13.2.0",
"uuid": "~3.2.1"
},
"devDependencies": {
Expand All @@ -40,7 +40,7 @@
"ts-jest": "^22.4.2",
"tslint": "^5.9.1",
"tslint-config-airbnb": "^5.8.0",
"typescript": "^2.7.2"
"typescript": "^2.8.3"
},
"engines": {
"node": ">=6.0.0"
Expand Down
16 changes: 8 additions & 8 deletions src/config/errors.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export const errors = {
INTERNAL_ERROR: { code: 'INTERNAL_ERROR', i18n: 'internal_error', message: 'An unkown error occurred' },
INVALID_INPUT: { code: 'INVALID_INPUT', i18n: 'invalid_input', message: 'Invalid input provided' },
AUTHENTICATION_FAILED: { code: 'AUTHENTICATION_FAILED', i18n: 'authentication_failed', message: 'Authentication failed' },
BAD_REQUEST: { code: 'BAD_REQUEST', i18n: 'bad_request', message: 'Bad request' },
MISSING_HEADERS: { code: 'MISSING_HEADERS', i18n: 'missing_headers', message: 'Missing headers' },
UNAUTHORIZED: { code: 'UNAUTHORIZED', i18n: 'unauthorized', message: 'Unauthorized' },
FORBIDDEN: { code: 'FORBIDDEN', i18n: 'forbidden', message: 'No access' },
RESOURCE_NOT_FOUND: { code: 'RESOURCE_NOT_FOUND', i18n: 'resource_not_found', message: 'Resource not found' },
INTERNAL_ERROR: { code: 'INTERNAL_ERROR',i18n: 'internal_error',message: 'An unkown error occurred' },
INVALID_INPUT: { code: 'INVALID_INPUT', i18n: 'invalid_input', message: 'Invalid input provided' },
AUTHENTICATION_FAILED: { code: 'AUTHENTICATION_FAILED', i18n: 'authentication_failed', message: 'Authentication failed' },
BAD_REQUEST: { code: 'BAD_REQUEST', i18n: 'bad_request', message: 'Bad request' },
MISSING_HEADERS: { code: 'MISSING_HEADERS', i18n: 'missing_headers', message: 'Missing headers' },
UNAUTHORIZED: { code: 'UNAUTHORIZED', i18n: 'unauthorized', message: 'Unauthorized' },
FORBIDDEN: { code: 'FORBIDDEN', i18n: 'forbidden', message: 'No access' },
RESOURCE_NOT_FOUND: { code: 'RESOURCE_NOT_FOUND', i18n: 'resource_not_found', message: 'Resource not found' },
};
14 changes: 7 additions & 7 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ export function parseErrors(error: any, i18n?: I18nOptions) {

// Own thrown ApiErrors
if (error instanceof ApiError) {
let translatedMessage = error.i18n; // TODO: Weird stuff??
let translatedMessage = error.message;

if (i18n) {
const translator = getTranslator(i18n.path, i18n.defaultLocale);
translator.setLocale(i18n.language);
translatedMessage = translator.__(error.i18n);
}

// if the translatedMessage equals the error code
// OR is undefined because not found
// fallback to default error message from ErrorConfig
if (translatedMessage === error.i18n || translatedMessage === undefined) {
translatedMessage = error.message;
// if the translatedMessage equals the error code OR is undefined because not found
// fallback to default error message from errors
if (translatedMessage === error.i18n || translatedMessage === undefined) {
translatedMessage = error.message;
}
}

parsedError = Object.assign({}, error, { message: translatedMessage });
}

Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
version "0.2.30"
resolved "https://registry.npmjs.org/@types/http-status/-/http-status-0.2.30.tgz#b43a1e1673b6ed9b5a28e8647862b51b6473634d"

"@types/i18n@0.8.3":
"@types/i18n@~0.8.3":
version "0.8.3"
resolved "https://registry.npmjs.org/@types/i18n/-/i18n-0.8.3.tgz#f602164f2fae486ea87590f6be5d6dd5db1664e6"

Expand Down Expand Up @@ -2778,9 +2778,9 @@ jest@^22.1.4:
import-local "^1.0.0"
jest-cli "^22.4.3"

joi@~13.1.2:
version "13.1.2"
resolved "https://registry.npmjs.org/joi/-/joi-13.1.2.tgz#b2db260323cc7f919fafa51e09e2275bd089a97e"
joi@~13.2.0:
version "13.2.0"
resolved "https://registry.npmjs.org/joi/-/joi-13.2.0.tgz#72307f1765bb40b068361f9368a4ba1092b8478e"
dependencies:
hoek "5.x.x"
isemail "3.x.x"
Expand Down Expand Up @@ -4985,9 +4985,9 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript@^2.7.2:
version "2.8.1"
resolved "https://registry.npmjs.org/typescript/-/typescript-2.8.1.tgz#6160e4f8f195d5ba81d4876f9c0cc1fbc0820624"
typescript@^2.8.3:
version "2.8.3"
resolved "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz#5d817f9b6f31bb871835f4edf0089f21abe6c170"

uglify-js@^2.6:
version "2.8.29"
Expand Down

0 comments on commit a6dddf2

Please sign in to comment.