Skip to content

Commit

Permalink
fix: CrowdinValidationError and handle single error
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Smolinskyi committed Nov 17, 2023
1 parent b3a405d commit e6f1770
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,48 @@ export class CrowdinError extends Error {
* @internal
*/
export class CrowdinValidationError extends CrowdinError {
public validationCodes: { key: string; codes: string[] }[];
constructor(messsage: string, validationCodes: { key: string; codes: string[] }[]) {
super(messsage, 400);
this.validationCodes = validationCodes;
public validationCodes?: { key: string; codes: string[] }[];
constructor({
message,
code,
validationCodes,
}: {
message: string;
code?: number;
validationCodes?: { key: string; codes: string[] }[];
}) {
super(message, code || 400);
this.validationCodes = validationCodes || [];
}
}

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

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

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
return !!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 158 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 158 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 161 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 161 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 161 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 161 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[] }[] = [];
const validationMessages: string[] = [];
crowdinResponseErrors.forEach((e: any) => {

Check warning on line 174 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 174 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
if (typeof e.index === 'number' && Array.isArray(e.errors)) {
throw new CrowdinValidationError(JSON.stringify(crowdinResponseErrors, null, 2), []);
throw new CrowdinValidationError({ message: JSON.stringify(crowdinResponseErrors, null, 2) });
}
if (e.error?.key && Array.isArray(e.error?.errors)) {
const codes: string[] = [];
Expand All @@ -171,8 +187,11 @@ export function handleHttpClientError(error: HttpClientError): never {
}
});
const message = validationMessages.length === 0 ? 'Validation error' : validationMessages.join(', ');
throw new CrowdinValidationError(message, validationCodes);
throw new CrowdinValidationError({ message, validationCodes });
} else if (crowdinResponseErrors?.message && crowdinResponseErrors?.code) {
throw new CrowdinValidationError({ ...crowdinResponseErrors });

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

View check run for this annotation

Codecov / codecov/patch

src/core/index.ts#L192

Added line #L192 was not covered by tests
}

if (error instanceof Error) {
const code =
error instanceof AxiosError && error.response?.status
Expand Down

0 comments on commit e6f1770

Please sign in to comment.