🥅(app-desk) add data to APIError

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.
This commit is contained in:
Anthony LC
2024-03-22 15:59:50 +01:00
committed by Anthony LC
parent 8b63807f38
commit bbf2695dee
2 changed files with 10 additions and 6 deletions

View File

@@ -1,17 +1,20 @@
interface IAPIError {
interface IAPIError<T = unknown> {
status: number;
cause?: string[];
data?: T;
}
export class APIError extends Error implements IAPIError {
public status: number;
public cause?: string[];
export class APIError<T = unknown> extends Error implements IAPIError<T> {
public status: IAPIError['status'];
public cause?: IAPIError['cause'];
public data?: IAPIError<T>['data'];
constructor(message: string, { status, cause }: IAPIError) {
constructor(message: string, { status, cause, data }: IAPIError<T>) {
super(message);
this.name = 'APIError';
this.status = status;
this.cause = cause;
this.data = data;
}
}

View File

@@ -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,
};
};