mirror of
https://github.com/formbricks/formbricks.git
synced 2026-05-06 19:35:53 -05:00
75 lines
1.6 KiB
TypeScript
75 lines
1.6 KiB
TypeScript
export type TV3InvalidParam = {
|
|
name: string;
|
|
reason: string;
|
|
};
|
|
|
|
type TV3ProblemBody = {
|
|
status?: number;
|
|
detail?: string;
|
|
code?: string;
|
|
requestId?: string;
|
|
invalid_params?: TV3InvalidParam[];
|
|
};
|
|
|
|
export class V3ApiError extends Error {
|
|
status: number;
|
|
code?: string;
|
|
requestId?: string;
|
|
invalid_params?: TV3InvalidParam[];
|
|
|
|
constructor({
|
|
status,
|
|
detail,
|
|
code,
|
|
requestId,
|
|
invalid_params,
|
|
}: {
|
|
status: number;
|
|
detail: string;
|
|
code?: string;
|
|
requestId?: string;
|
|
invalid_params?: TV3InvalidParam[];
|
|
}) {
|
|
super(detail);
|
|
this.name = "V3ApiError";
|
|
this.status = status;
|
|
this.code = code;
|
|
this.requestId = requestId;
|
|
this.invalid_params = invalid_params;
|
|
}
|
|
|
|
get detail(): string {
|
|
return this.message;
|
|
}
|
|
}
|
|
|
|
export function getV3ApiErrorMessage(error: unknown, fallbackMessage: string): string {
|
|
if (error instanceof V3ApiError) {
|
|
return error.detail;
|
|
}
|
|
|
|
if (error instanceof Error && error.message) {
|
|
return error.message;
|
|
}
|
|
|
|
return fallbackMessage;
|
|
}
|
|
|
|
export async function parseV3ApiError(response: Response): Promise<V3ApiError> {
|
|
let problemBody: TV3ProblemBody | undefined;
|
|
|
|
try {
|
|
problemBody = (await response.json()) as TV3ProblemBody;
|
|
} catch {
|
|
problemBody = undefined;
|
|
}
|
|
|
|
return new V3ApiError({
|
|
status: problemBody?.status ?? response.status,
|
|
detail: problemBody?.detail ?? response.statusText ?? "An unexpected error occurred.",
|
|
code: problemBody?.code,
|
|
requestId: problemBody?.requestId ?? response.headers.get("X-Request-Id") ?? undefined,
|
|
invalid_params: problemBody?.invalid_params,
|
|
});
|
|
}
|