2023-02-18 12:40:37 +03:00
|
|
|
import type {
|
2022-10-12 11:35:16 +03:00
|
|
|
InvalidArgumentError,
|
|
|
|
InvalidShortUrlDeletion,
|
|
|
|
ProblemDetailsError,
|
2023-02-18 12:40:37 +03:00
|
|
|
RegularNotFound } from '../types/errors';
|
|
|
|
import {
|
|
|
|
ErrorTypeV2,
|
|
|
|
ErrorTypeV3,
|
2022-10-12 11:35:16 +03:00
|
|
|
} 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 =>
|
2022-11-15 14:15:04 +03:00
|
|
|
!!e && typeof e === 'object' && ['type', 'detail', 'title', 'status'].every((prop) => prop in e);
|
2022-11-13 18:57:16 +03:00
|
|
|
|
2022-11-15 13:41:05 +03:00
|
|
|
export const parseApiError = (e: unknown): ProblemDetailsError | undefined => (isProblemDetails(e) ? e : 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;
|