From 740849dd89e3746fdc34c3a112288dbfe4bd7220 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 28 Feb 2023 13:51:18 -0300 Subject: better error handling on unknown error --- packages/web-util/src/utils/request.ts | 100 ++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 25 deletions(-) (limited to 'packages/web-util') diff --git a/packages/web-util/src/utils/request.ts b/packages/web-util/src/utils/request.ts index 3d91024dc..c7bca2de9 100644 --- a/packages/web-util/src/utils/request.ts +++ b/packages/web-util/src/utils/request.ts @@ -18,15 +18,18 @@ import { HttpStatusCode } from "@gnu-taler/taler-util"; import { base64encode } from "./base64.js"; export enum ErrorType { - CLIENT, SERVER, TIMEOUT, UNEXPECTED + CLIENT, + SERVER, + TIMEOUT, + UNEXPECTED, } /** - * + * * @param baseUrl URL where the service is located - * @param endpoint endpoint of the service to be called + * @param endpoint endpoint of the service to be called * @param options auth, method and params - * @returns + * @returns */ export async function defaultRequestHandler( baseUrl: string, @@ -35,11 +38,14 @@ export async function defaultRequestHandler( ): Promise> { const requestHeaders: Record = {}; if (options.token) { - requestHeaders.Authorization = `Bearer ${options.token}` + requestHeaders.Authorization = `Bearer ${options.token}`; } else if (options.basicAuth) { - requestHeaders.Authorization = `Basic ${base64encode(`${options.basicAuth.username}:${options.basicAuth.password}`)}` + requestHeaders.Authorization = `Basic ${base64encode( + `${options.basicAuth.username}:${options.basicAuth.password}`, + )}`; } - requestHeaders["Content-Type"] = options.contentType === "json" ? "application/json" : "text/plain" + requestHeaders["Content-Type"] = + options.contentType === "json" ? "application/json" : "text/plain"; const requestMethod = options?.method ?? "GET"; const requestBody = options?.data; @@ -178,15 +184,23 @@ export type HttpError = | HttpResponseServerError | HttpResponseUnexpectedError; - export interface HttpResponseServerError { ok?: false; loading?: false; + /** + * @deprecated use status + */ clientError?: false; + /** + * @deprecated use status + */ serverError: true; - type: ErrorType.SERVER, - + type: ErrorType.SERVER; + /** + * @deprecated use payload + */ error: ErrorDetail; + payload: ErrorDetail; status: HttpStatusCode; message: string; info?: RequestInfo; @@ -194,12 +208,18 @@ export interface HttpResponseServerError { interface HttpRequestTimeoutError { ok?: false; loading?: false; + /** + * @deprecated use type + */ clientError: true; + /** + * @deprecated use type + */ serverError?: false; - type: ErrorType.TIMEOUT, + type: ErrorType.TIMEOUT; info?: RequestInfo; - error: undefined, + error: undefined; isUnauthorized: false; isNotfound: false; @@ -208,28 +228,54 @@ interface HttpRequestTimeoutError { interface HttpResponseClientError { ok?: false; loading?: false; + /** + * @deprecated use type + */ clientError: true; + /** + * @deprecated use type + */ serverError?: false; - type: ErrorType.CLIENT, + type: ErrorType.CLIENT; info?: RequestInfo; + /** + * @deprecated use status + */ isUnauthorized: boolean; + /** + * @deprecated use status + */ isNotfound: boolean; status: HttpStatusCode; + /** + * @deprecated use payload + */ error: ErrorDetail; + payload: ErrorDetail; message: string; } interface HttpResponseUnexpectedError { ok?: false; loading?: false; + /** + * @deprecated use type + */ clientError?: false; + /** + * @deprecated use type + */ serverError?: false; - type: ErrorType.UNEXPECTED, + type: ErrorType.UNEXPECTED; info?: RequestInfo; status?: HttpStatusCode; + /** + * @deprecated use exception + */ error: unknown; + exception: unknown; message: string; } @@ -240,9 +286,9 @@ export class RequestError extends Error { info: HttpError; cause: HttpError; constructor(d: HttpError) { - super(d.message) - this.info = d - this.cause = d + super(d.message); + this.info = d; + this.cause = d; } } @@ -252,13 +298,13 @@ export interface RequestOptions { method?: Methods; token?: string; basicAuth?: { - username: string, - password: string, - } + username: string; + password: string; + }; data?: any; params?: unknown; - timeout?: number, - contentType?: "text" | "json" + timeout?: number; + contentType?: "text" | "json"; } async function buildRequestOk( @@ -312,7 +358,8 @@ async function buildRequestFailed( status, info, message: data?.hint, - error: data, + error: data, // remove this + payload: data, }; return error; } @@ -323,7 +370,8 @@ async function buildRequestFailed( status, info, message: `${data?.hint} (code ${data?.code})`, - error: data, + error: data, //remove this + payload: data, }; return error; } @@ -331,7 +379,8 @@ async function buildRequestFailed( info, type: ErrorType.UNEXPECTED, status, - error: {}, + error: {}, // remove this + exception: undefined, message: "NOT DEFINED", }; } catch (ex) { @@ -340,6 +389,7 @@ async function buildRequestFailed( status, type: ErrorType.UNEXPECTED, error: ex, + exception: ex, message: "NOT DEFINED", }; -- cgit v1.2.3