Skip to content

Commit

Permalink
fix: CrowdinValidationError and handle single error (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
VBeytok authored Nov 21, 2023
1 parent da3ad74 commit 27f342f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,32 @@ export class CrowdinError extends Error {
*/
export class CrowdinValidationError extends CrowdinError {
public validationCodes: { key: string; codes: string[] }[];
constructor(messsage: string, validationCodes: { key: string; codes: string[] }[]) {
super(messsage, 400);
constructor(message: string, validationCodes: { key: string; codes: string[] }[]) {
super(message, 400);
this.validationCodes = validationCodes;
}
}

function isAxiosError(error: any): error is AxiosError {

Check warning on line 142 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
return error instanceof AxiosError || !!error.response?.data;
}

/**
* @internal
*/
export function handleHttpClientError(error: HttpClientError): never {
const crowdinResponseErrors =
error instanceof AxiosError
? error.response?.data?.errors
: error instanceof FetchClientJsonPayloadError
? error.jsonPayload && typeof error.jsonPayload === 'object' && 'errors' in error.jsonPayload
? error.jsonPayload.errors
: null
: null;
let crowdinResponseErrors: any = null;

Check warning on line 150 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

if (isAxiosError(error)) {
crowdinResponseErrors = (error.response?.data as any)?.errors || (error.response?.data as any)?.error;

Check warning on line 153 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 153 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
} else if (error instanceof FetchClientJsonPayloadError) {
crowdinResponseErrors =
error.jsonPayload &&
typeof error.jsonPayload === 'object' &&
('errors' in error.jsonPayload || 'error' in error.jsonPayload)
? error.jsonPayload.errors || error.jsonPayload.error
: null;
}

if (Array.isArray(crowdinResponseErrors)) {
const validationCodes: { key: string; codes: string[] }[] = [];
Expand All @@ -172,7 +180,10 @@ export function handleHttpClientError(error: HttpClientError): never {
});
const message = validationMessages.length === 0 ? 'Validation error' : validationMessages.join(', ');
throw new CrowdinValidationError(message, validationCodes);
} else if (crowdinResponseErrors?.message && crowdinResponseErrors?.code) {
throw new CrowdinError(crowdinResponseErrors.message, crowdinResponseErrors.code);
}

if (error instanceof Error) {
const code =
error instanceof AxiosError && error.response?.status
Expand Down
12 changes: 12 additions & 0 deletions tests/core/error-handling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const genericCrowdinErrorPayload = {
],
};

const genericCrowdinSingleErrorPayload = {
error: {
message: 'test_errors_error_msg',
code: 403,
},
};

const stringBatchOperationsErrorPayload = {
errors: [
{
Expand Down Expand Up @@ -88,6 +95,11 @@ describe('core http error handling', () => {
}
});

it('should extract Crowdin API single message with axios client', async () => {
const error = createAxiosError(genericCrowdinSingleErrorPayload);
expect(() => handleHttpClientError(error)).toThrowError(genericCrowdinSingleErrorPayload.error.message);
});

it('should print full error message for stringBatchOperations axios errors', async () => {
const error = createAxiosError(stringBatchOperationsErrorPayload);
expect(() => handleHttpClientError(error)).toThrowError(
Expand Down

0 comments on commit 27f342f

Please sign in to comment.