🥅(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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user