better error handling on unknown error
This commit is contained in:
parent
04eec324bf
commit
740849dd89
@ -18,15 +18,18 @@ import { HttpStatusCode } from "@gnu-taler/taler-util";
|
|||||||
import { base64encode } from "./base64.js";
|
import { base64encode } from "./base64.js";
|
||||||
|
|
||||||
export enum ErrorType {
|
export enum ErrorType {
|
||||||
CLIENT, SERVER, TIMEOUT, UNEXPECTED
|
CLIENT,
|
||||||
|
SERVER,
|
||||||
|
TIMEOUT,
|
||||||
|
UNEXPECTED,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param baseUrl URL where the service is located
|
* @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
|
* @param options auth, method and params
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export async function defaultRequestHandler<T>(
|
export async function defaultRequestHandler<T>(
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
@ -35,11 +38,14 @@ export async function defaultRequestHandler<T>(
|
|||||||
): Promise<HttpResponseOk<T>> {
|
): Promise<HttpResponseOk<T>> {
|
||||||
const requestHeaders: Record<string, string> = {};
|
const requestHeaders: Record<string, string> = {};
|
||||||
if (options.token) {
|
if (options.token) {
|
||||||
requestHeaders.Authorization = `Bearer ${options.token}`
|
requestHeaders.Authorization = `Bearer ${options.token}`;
|
||||||
} else if (options.basicAuth) {
|
} 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 requestMethod = options?.method ?? "GET";
|
||||||
const requestBody = options?.data;
|
const requestBody = options?.data;
|
||||||
@ -178,15 +184,23 @@ export type HttpError<ErrorDetail> =
|
|||||||
| HttpResponseServerError<ErrorDetail>
|
| HttpResponseServerError<ErrorDetail>
|
||||||
| HttpResponseUnexpectedError;
|
| HttpResponseUnexpectedError;
|
||||||
|
|
||||||
|
|
||||||
export interface HttpResponseServerError<ErrorDetail> {
|
export interface HttpResponseServerError<ErrorDetail> {
|
||||||
ok?: false;
|
ok?: false;
|
||||||
loading?: false;
|
loading?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use status
|
||||||
|
*/
|
||||||
clientError?: false;
|
clientError?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use status
|
||||||
|
*/
|
||||||
serverError: true;
|
serverError: true;
|
||||||
type: ErrorType.SERVER,
|
type: ErrorType.SERVER;
|
||||||
|
/**
|
||||||
|
* @deprecated use payload
|
||||||
|
*/
|
||||||
error: ErrorDetail;
|
error: ErrorDetail;
|
||||||
|
payload: ErrorDetail;
|
||||||
status: HttpStatusCode;
|
status: HttpStatusCode;
|
||||||
message: string;
|
message: string;
|
||||||
info?: RequestInfo;
|
info?: RequestInfo;
|
||||||
@ -194,12 +208,18 @@ export interface HttpResponseServerError<ErrorDetail> {
|
|||||||
interface HttpRequestTimeoutError {
|
interface HttpRequestTimeoutError {
|
||||||
ok?: false;
|
ok?: false;
|
||||||
loading?: false;
|
loading?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
clientError: true;
|
clientError: true;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
serverError?: false;
|
serverError?: false;
|
||||||
type: ErrorType.TIMEOUT,
|
type: ErrorType.TIMEOUT;
|
||||||
|
|
||||||
info?: RequestInfo;
|
info?: RequestInfo;
|
||||||
error: undefined,
|
error: undefined;
|
||||||
|
|
||||||
isUnauthorized: false;
|
isUnauthorized: false;
|
||||||
isNotfound: false;
|
isNotfound: false;
|
||||||
@ -208,28 +228,54 @@ interface HttpRequestTimeoutError {
|
|||||||
interface HttpResponseClientError<ErrorDetail> {
|
interface HttpResponseClientError<ErrorDetail> {
|
||||||
ok?: false;
|
ok?: false;
|
||||||
loading?: false;
|
loading?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
clientError: true;
|
clientError: true;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
serverError?: false;
|
serverError?: false;
|
||||||
type: ErrorType.CLIENT,
|
type: ErrorType.CLIENT;
|
||||||
|
|
||||||
info?: RequestInfo;
|
info?: RequestInfo;
|
||||||
|
/**
|
||||||
|
* @deprecated use status
|
||||||
|
*/
|
||||||
isUnauthorized: boolean;
|
isUnauthorized: boolean;
|
||||||
|
/**
|
||||||
|
* @deprecated use status
|
||||||
|
*/
|
||||||
isNotfound: boolean;
|
isNotfound: boolean;
|
||||||
status: HttpStatusCode;
|
status: HttpStatusCode;
|
||||||
|
/**
|
||||||
|
* @deprecated use payload
|
||||||
|
*/
|
||||||
error: ErrorDetail;
|
error: ErrorDetail;
|
||||||
|
payload: ErrorDetail;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface HttpResponseUnexpectedError {
|
interface HttpResponseUnexpectedError {
|
||||||
ok?: false;
|
ok?: false;
|
||||||
loading?: false;
|
loading?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
clientError?: false;
|
clientError?: false;
|
||||||
|
/**
|
||||||
|
* @deprecated use type
|
||||||
|
*/
|
||||||
serverError?: false;
|
serverError?: false;
|
||||||
type: ErrorType.UNEXPECTED,
|
type: ErrorType.UNEXPECTED;
|
||||||
|
|
||||||
info?: RequestInfo;
|
info?: RequestInfo;
|
||||||
status?: HttpStatusCode;
|
status?: HttpStatusCode;
|
||||||
|
/**
|
||||||
|
* @deprecated use exception
|
||||||
|
*/
|
||||||
error: unknown;
|
error: unknown;
|
||||||
|
exception: unknown;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -240,9 +286,9 @@ export class RequestError<ErrorDetail> extends Error {
|
|||||||
info: HttpError<ErrorDetail>;
|
info: HttpError<ErrorDetail>;
|
||||||
cause: HttpError<ErrorDetail>;
|
cause: HttpError<ErrorDetail>;
|
||||||
constructor(d: HttpError<ErrorDetail>) {
|
constructor(d: HttpError<ErrorDetail>) {
|
||||||
super(d.message)
|
super(d.message);
|
||||||
this.info = d
|
this.info = d;
|
||||||
this.cause = d
|
this.cause = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,13 +298,13 @@ export interface RequestOptions {
|
|||||||
method?: Methods;
|
method?: Methods;
|
||||||
token?: string;
|
token?: string;
|
||||||
basicAuth?: {
|
basicAuth?: {
|
||||||
username: string,
|
username: string;
|
||||||
password: string,
|
password: string;
|
||||||
}
|
};
|
||||||
data?: any;
|
data?: any;
|
||||||
params?: unknown;
|
params?: unknown;
|
||||||
timeout?: number,
|
timeout?: number;
|
||||||
contentType?: "text" | "json"
|
contentType?: "text" | "json";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function buildRequestOk<T>(
|
async function buildRequestOk<T>(
|
||||||
@ -312,7 +358,8 @@ async function buildRequestFailed<ErrorDetail>(
|
|||||||
status,
|
status,
|
||||||
info,
|
info,
|
||||||
message: data?.hint,
|
message: data?.hint,
|
||||||
error: data,
|
error: data, // remove this
|
||||||
|
payload: data,
|
||||||
};
|
};
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@ -323,7 +370,8 @@ async function buildRequestFailed<ErrorDetail>(
|
|||||||
status,
|
status,
|
||||||
info,
|
info,
|
||||||
message: `${data?.hint} (code ${data?.code})`,
|
message: `${data?.hint} (code ${data?.code})`,
|
||||||
error: data,
|
error: data, //remove this
|
||||||
|
payload: data,
|
||||||
};
|
};
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
@ -331,7 +379,8 @@ async function buildRequestFailed<ErrorDetail>(
|
|||||||
info,
|
info,
|
||||||
type: ErrorType.UNEXPECTED,
|
type: ErrorType.UNEXPECTED,
|
||||||
status,
|
status,
|
||||||
error: {},
|
error: {}, // remove this
|
||||||
|
exception: undefined,
|
||||||
message: "NOT DEFINED",
|
message: "NOT DEFINED",
|
||||||
};
|
};
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@ -340,6 +389,7 @@ async function buildRequestFailed<ErrorDetail>(
|
|||||||
status,
|
status,
|
||||||
type: ErrorType.UNEXPECTED,
|
type: ErrorType.UNEXPECTED,
|
||||||
error: ex,
|
error: ex,
|
||||||
|
exception: ex,
|
||||||
message: "NOT DEFINED",
|
message: "NOT DEFINED",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user