diff options
author | Sebastian <sebasjm@gmail.com> | 2023-02-28 13:51:18 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-02-28 13:51:18 -0300 |
commit | 740849dd89e3746fdc34c3a112288dbfe4bd7220 (patch) | |
tree | 49c151b9aac1bb9b861bd01deb60356a660ebfb8 /packages/web-util | |
parent | 04eec324bff7601a89a1669ea6edfc699d698595 (diff) |
better error handling on unknown error
Diffstat (limited to 'packages/web-util')
-rw-r--r-- | packages/web-util/src/utils/request.ts | 100 |
1 files changed, 75 insertions, 25 deletions
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<T>( baseUrl: string, @@ -35,11 +38,14 @@ export async function defaultRequestHandler<T>( ): Promise<HttpResponseOk<T>> { const requestHeaders: Record<string, string> = {}; 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<ErrorDetail> = | HttpResponseServerError<ErrorDetail> | HttpResponseUnexpectedError; - export interface HttpResponseServerError<ErrorDetail> { 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<ErrorDetail> { 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<ErrorDetail> { 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<ErrorDetail> extends Error { info: HttpError<ErrorDetail>; cause: HttpError<ErrorDetail>; constructor(d: HttpError<ErrorDetail>) { - 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<T>( @@ -312,7 +358,8 @@ async function buildRequestFailed<ErrorDetail>( status, info, message: data?.hint, - error: data, + error: data, // remove this + payload: data, }; return error; } @@ -323,7 +370,8 @@ async function buildRequestFailed<ErrorDetail>( 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<ErrorDetail>( info, type: ErrorType.UNEXPECTED, status, - error: {}, + error: {}, // remove this + exception: undefined, message: "NOT DEFINED", }; } catch (ex) { @@ -340,6 +389,7 @@ async function buildRequestFailed<ErrorDetail>( status, type: ErrorType.UNEXPECTED, error: ex, + exception: ex, message: "NOT DEFINED", }; |