2022-10-12 10:35:16 +02:00
|
|
|
import {
|
|
|
|
ErrorTypeV2,
|
|
|
|
ErrorTypeV3,
|
|
|
|
InvalidArgumentError,
|
|
|
|
InvalidShortUrlDeletion,
|
|
|
|
ProblemDetailsError,
|
|
|
|
RegularNotFound,
|
|
|
|
} from '../types/errors';
|
2020-12-21 23:51:35 +01:00
|
|
|
|
2022-11-13 16:57:16 +01: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-15 11:41:05 +01:00
|
|
|
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => (isProblemDetails(e) ? e : undefined);
|
2020-12-22 09:49:13 +01:00
|
|
|
|
|
|
|
export const isInvalidArgumentError = (error?: ProblemDetailsError): error is InvalidArgumentError =>
|
2022-10-12 10:35:16 +02:00
|
|
|
error?.type === ErrorTypeV2.INVALID_ARGUMENT || error?.type === ErrorTypeV3.INVALID_ARGUMENT;
|
2020-12-22 09:49:13 +01:00
|
|
|
|
|
|
|
export const isInvalidDeletionError = (error?: ProblemDetailsError): error is InvalidShortUrlDeletion =>
|
2022-10-12 10:35:16 +02:00
|
|
|
error?.type === 'INVALID_SHORTCODE_DELETION'
|
|
|
|
|| error?.type === ErrorTypeV2.INVALID_SHORT_URL_DELETION
|
|
|
|
|| error?.type === ErrorTypeV3.INVALID_SHORT_URL_DELETION;
|
2022-10-12 10:19:54 +02:00
|
|
|
|
|
|
|
export const isRegularNotFound = (error?: ProblemDetailsError): error is RegularNotFound =>
|
2022-10-12 10:35:16 +02:00
|
|
|
(error?.type === ErrorTypeV2.NOT_FOUND || error?.type === ErrorTypeV3.NOT_FOUND) && error?.status === 404;
|