2020-12-22 01:51:35 +03:00
|
|
|
import { AxiosError } from 'axios';
|
2022-10-12 11:35:16 +03:00
|
|
|
import {
|
|
|
|
ErrorTypeV2,
|
|
|
|
ErrorTypeV3,
|
|
|
|
InvalidArgumentError,
|
|
|
|
InvalidShortUrlDeletion,
|
|
|
|
ProblemDetailsError,
|
|
|
|
RegularNotFound,
|
|
|
|
} from '../types/errors';
|
2020-12-22 01:51:35 +03:00
|
|
|
|
2022-11-13 18:57:16 +03:00
|
|
|
const isProblemDetails = (e: unknown): e is ProblemDetailsError =>
|
|
|
|
!!e && typeof e === 'object' && Object.keys(e).every((key) => ['type', 'detail', 'title', 'status'].includes(key));
|
|
|
|
|
2022-11-03 22:51:20 +03:00
|
|
|
const isAxiosError = (e: unknown): e is AxiosError<ProblemDetailsError> => !!e && typeof e === 'object' && 'response' in e;
|
|
|
|
|
2022-11-13 18:57:16 +03:00
|
|
|
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => {
|
|
|
|
if (isProblemDetails(e)) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (isAxiosError(e) ? e.response?.data : undefined);
|
|
|
|
};
|
2020-12-22 11:49:13 +03:00
|
|
|
|
|
|
|
export const isInvalidArgumentError = (error?: ProblemDetailsError): error is InvalidArgumentError =>
|
2022-10-12 11:35:16 +03:00
|
|
|
error?.type === ErrorTypeV2.INVALID_ARGUMENT || error?.type === ErrorTypeV3.INVALID_ARGUMENT;
|
2020-12-22 11:49:13 +03:00
|
|
|
|
|
|
|
export const isInvalidDeletionError = (error?: ProblemDetailsError): error is InvalidShortUrlDeletion =>
|
2022-10-12 11:35:16 +03:00
|
|
|
error?.type === 'INVALID_SHORTCODE_DELETION'
|
|
|
|
|| error?.type === ErrorTypeV2.INVALID_SHORT_URL_DELETION
|
|
|
|
|| error?.type === ErrorTypeV3.INVALID_SHORT_URL_DELETION;
|
2022-10-12 11:19:54 +03:00
|
|
|
|
|
|
|
export const isRegularNotFound = (error?: ProblemDetailsError): error is RegularNotFound =>
|
2022-10-12 11:35:16 +03:00
|
|
|
(error?.type === ErrorTypeV2.NOT_FOUND || error?.type === ErrorTypeV3.NOT_FOUND) && error?.status === 404;
|