Skip to content

Commit

Permalink
feat: add isPutioAPIErrorData support to localizedError
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Nov 18, 2023
1 parent ede54b5 commit fb12046
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/localized-error/localizeError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ describe('localizeError', () => {
]);

expect(localizeError(error)).toBeInstanceOf(LocalizedError);
expect(localizeError(error.data)).toBeInstanceOf(LocalizedError);

error.data.status_code = 401;
expect(localizeError(error)).toBeInstanceOf(LocalizedError);
expect(localizeError(error.data)).toBeInstanceOf(LocalizedError);
});

it('shoud localize api errors by error_type', () => {
Expand All @@ -81,9 +83,11 @@ describe('localizeError', () => {
]);

expect(localizeError(error)).toBeInstanceOf(LocalizedError);
expect(localizeError(error.data)).toBeInstanceOf(LocalizedError);

error.data.error_type = 'test_new';
expect(localizeError(error)).toBeInstanceOf(LocalizedError);
expect(localizeError(error.data)).toBeInstanceOf(LocalizedError);
});

it('shoud localize api errors by match_condition', () => {
Expand Down
23 changes: 18 additions & 5 deletions src/localized-error/localizeError.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { IPutioAPIClientError, isPutioAPIError } from '@putdotio/api-client';
import { LocalizedError, LocalizedErrorParams } from './LocalizedError';
import {
type IPutioAPIClientError,
type IPutioAPIClientErrorData,
isPutioAPIError,
isPutioAPIErrorResponse,
} from '@putdotio/api-client';
import { LocalizedError, type LocalizedErrorParams } from './LocalizedError';

export type LocalizeFn<E> = (
error: E
Expand All @@ -8,13 +13,13 @@ export type LocalizeFn<E> = (
export type APIErrorByStatusCodeLocalizer = {
kind: 'api_status_code';
status_code: number;
localize: LocalizeFn<IPutioAPIClientError>;
localize: LocalizeFn<IPutioAPIClientError | IPutioAPIClientErrorData>;
};

export type APIErrorByErrorTypeLocalizer = {
kind: 'api_error_type';
error_type: string;
localize: LocalizeFn<IPutioAPIClientError>;
localize: LocalizeFn<IPutioAPIClientError | IPutioAPIClientErrorData>;
};

export type MatchConditionLocalizer<E> = {
Expand Down Expand Up @@ -51,10 +56,14 @@ export const createLocalizeError =
const localizers = [...scopedLocalizers, ...globalLocalizers];

// API ERROR
if (isPutioAPIError(error)) {
if (isPutioAPIError(error) || isPutioAPIErrorResponse(error)) {
const byErrorType = localizers.find(
(l): l is APIErrorByErrorTypeLocalizer => {
if (l.kind === 'api_error_type') {
if (isPutioAPIErrorResponse(error)) {
return l.error_type === error.error_type;
}

return l.error_type === error.data.error_type;
}

Expand All @@ -72,6 +81,10 @@ export const createLocalizeError =
const byStatusCode = localizers.find(
(l): l is APIErrorByStatusCodeLocalizer => {
if (l.kind === 'api_status_code') {
if (isPutioAPIErrorResponse(error)) {
return l.status_code === error.status_code;
}

return l.status_code === error.data.status_code;
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1458,10 +1458,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@putdotio/api-client@8.39.0":
version "8.39.0"
resolved "https://registry.yarnpkg.com/@putdotio/api-client/-/api-client-8.39.0.tgz#811b5ee0bbf895431d8ba695cab50d0cbde894af"
integrity sha512-xQJ+tuKmTo/gy+WFat86Avhqy8mlIZF2ZpbzBQc8173LTvRcrf9E7B8dKqz/P0pjLn+kM/IkiKwmudV9Q8NXdw==
"@putdotio/api-client@8.40.0":
version "8.40.0"
resolved "https://registry.yarnpkg.com/@putdotio/api-client/-/api-client-8.40.0.tgz#049d793b0ef88426efbf20f0485bca6156c414e5"
integrity sha512-iPx7OWeafK/X6h2LZj20865fMWip7gZMxWeNCbs/m/j+B5zI7bGWTPK6mJivDBAcIvuMzYy4TqcG/t7PEVxQ+A==
dependencies:
axios "^0.21.1"
event-emitter "^0.3.5"
Expand Down

0 comments on commit fb12046

Please sign in to comment.