Skip to content

Commit

Permalink
added error handling for validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Apr 23, 2022
1 parent 25cc887 commit e609105
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crowdin/crowdin-api-client",
"version": "1.17.1",
"version": "1.18.0",
"description": "JavaScript library for Crowdin API v2.",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
25 changes: 24 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,33 @@ export class CrowdinError extends Error {
}
}

export class CrowdinValidationError extends CrowdinError {
public validationCodes: string[];
constructor(messsage: string, validationCodes: string[]) {
super(messsage, 400);
this.validationCodes = validationCodes;
}
}

function handleError<T>(error: any = {}): T {
if (Array.isArray(error.errors)) {
const validationCodes: string[] = [];
const validationMessages: string[] = [];
error.errors.forEach((e: any) => {
if (Array.isArray(e.error?.errors)) {
e.error.errors.forEach((er: any) => {
if (er.message && er.code) {
validationCodes.push(er.code);
validationMessages.push(er.message);
}
});
}
});
const message = validationMessages.length === 0 ? 'Validation error' : validationMessages.join(', ');
throw new CrowdinValidationError(message, validationCodes);
}
const message = error.error?.message || 'Error occured';
const code = error.error?.code || 500;
//TODO handle validation errors
throw new CrowdinError(message, code);
}

Expand Down

0 comments on commit e609105

Please sign in to comment.