diff options
Diffstat (limited to 'packages/web-util/src/utils')
-rw-r--r-- | packages/web-util/src/utils/request.ts | 47 |
1 files changed, 41 insertions, 6 deletions
diff --git a/packages/web-util/src/utils/request.ts b/packages/web-util/src/utils/request.ts index 24342bb80..3d91024dc 100644 --- a/packages/web-util/src/utils/request.ts +++ b/packages/web-util/src/utils/request.ts @@ -17,6 +17,10 @@ import { HttpStatusCode } from "@gnu-taler/taler-util"; import { base64encode } from "./base64.js"; +export enum ErrorType { + CLIENT, SERVER, TIMEOUT, UNEXPECTED +} + /** * * @param baseUrl URL where the service is located @@ -39,7 +43,7 @@ export async function defaultRequestHandler<T>( const requestMethod = options?.method ?? "GET"; const requestBody = options?.data; - const requestTimeout = options?.timeout ?? 2 * 1000; + const requestTimeout = options?.timeout ?? 5 * 1000; const requestParams = options.params ?? {}; const _url = new URL(`${baseUrl}${endpoint}`); @@ -85,10 +89,13 @@ export async function defaultRequestHandler<T>( hasToken: !!options.token, status: 0, }; - const error: HttpResponseUnexpectedError = { + const error: HttpRequestTimeoutError = { + clientError: true, + isNotfound: false, + isUnauthorized: false, + error: undefined, info, - status: 0, - error: ex, + type: ErrorType.TIMEOUT, message: "Request timeout", }; throw new RequestError(error); @@ -166,32 +173,50 @@ export interface WithPagination { } export type HttpError<ErrorDetail> = + | HttpRequestTimeoutError | HttpResponseClientError<ErrorDetail> | HttpResponseServerError<ErrorDetail> | HttpResponseUnexpectedError; + export interface HttpResponseServerError<ErrorDetail> { ok?: false; loading?: false; clientError?: false; serverError: true; + type: ErrorType.SERVER, - error?: ErrorDetail; + error: ErrorDetail; status: HttpStatusCode; message: string; info?: RequestInfo; } +interface HttpRequestTimeoutError { + ok?: false; + loading?: false; + clientError: true; + serverError?: false; + type: ErrorType.TIMEOUT, + + info?: RequestInfo; + error: undefined, + + isUnauthorized: false; + isNotfound: false; + message: string; +} interface HttpResponseClientError<ErrorDetail> { ok?: false; loading?: false; clientError: true; serverError?: false; + type: ErrorType.CLIENT, info?: RequestInfo; isUnauthorized: boolean; isNotfound: boolean; status: HttpStatusCode; - error?: ErrorDetail; + error: ErrorDetail; message: string; } @@ -200,6 +225,7 @@ interface HttpResponseUnexpectedError { loading?: false; clientError?: false; serverError?: false; + type: ErrorType.UNEXPECTED, info?: RequestInfo; status?: HttpStatusCode; @@ -208,10 +234,15 @@ interface HttpResponseUnexpectedError { } export class RequestError<ErrorDetail> extends Error { + /** + * @deprecated use cause + */ info: HttpError<ErrorDetail>; + cause: HttpError<ErrorDetail>; constructor(d: HttpError<ErrorDetail>) { super(d.message) this.info = d + this.cause = d } } @@ -277,6 +308,7 @@ async function buildRequestFailed<ErrorDetail>( clientError: true, isNotfound: status === 404, isUnauthorized: status === 401, + type: ErrorType.CLIENT, status, info, message: data?.hint, @@ -287,6 +319,7 @@ async function buildRequestFailed<ErrorDetail>( if (status && status >= 500 && status < 600) { const error: HttpResponseServerError<ErrorDetail> = { serverError: true, + type: ErrorType.SERVER, status, info, message: `${data?.hint} (code ${data?.code})`, @@ -296,6 +329,7 @@ async function buildRequestFailed<ErrorDetail>( } return { info, + type: ErrorType.UNEXPECTED, status, error: {}, message: "NOT DEFINED", @@ -304,6 +338,7 @@ async function buildRequestFailed<ErrorDetail>( const error: HttpResponseUnexpectedError = { info, status, + type: ErrorType.UNEXPECTED, error: ex, message: "NOT DEFINED", }; |