From bbf2695dee46c06d1791436157fb1ba3943c6d87 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 22 Mar 2024 15:59:50 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85(app-desk)=20add=20data=20to=20APIE?= =?UTF-8?q?rror?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We sometime need to get some data back when an error occurs. We add a data prop to the APIError class to allow us to do that. --- src/frontend/apps/desk/src/api/APIError.ts | 13 ++++++++----- src/frontend/apps/desk/src/api/utils.ts | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/frontend/apps/desk/src/api/APIError.ts b/src/frontend/apps/desk/src/api/APIError.ts index a94adb4..2aaf715 100644 --- a/src/frontend/apps/desk/src/api/APIError.ts +++ b/src/frontend/apps/desk/src/api/APIError.ts @@ -1,17 +1,20 @@ -interface IAPIError { +interface IAPIError { status: number; cause?: string[]; + data?: T; } -export class APIError extends Error implements IAPIError { - public status: number; - public cause?: string[]; +export class APIError extends Error implements IAPIError { + public status: IAPIError['status']; + public cause?: IAPIError['cause']; + public data?: IAPIError['data']; - constructor(message: string, { status, cause }: IAPIError) { + constructor(message: string, { status, cause, data }: IAPIError) { super(message); this.name = 'APIError'; this.status = status; this.cause = cause; + this.data = data; } } diff --git a/src/frontend/apps/desk/src/api/utils.ts b/src/frontend/apps/desk/src/api/utils.ts index 6be3c02..5a4370e 100644 --- a/src/frontend/apps/desk/src/api/utils.ts +++ b/src/frontend/apps/desk/src/api/utils.ts @@ -1,4 +1,4 @@ -export const errorCauses = async (response: Response) => { +export const errorCauses = async (response: Response, data?: unknown) => { const errorsBody = (await response.json()) as Record< string, string | string[] @@ -13,5 +13,6 @@ export const errorCauses = async (response: Response) => { return { status: response.status, cause: causes, + data, }; };