aboutsummaryrefslogtreecommitdiff
path: root/packages/web-util/src/utils
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-04-07 18:46:25 -0300
committerSebastian <sebasjm@gmail.com>2023-04-07 18:46:25 -0300
commitcfe7129c4ecaa3057d5e47040f0a9efd8ed0317e (patch)
treec4a282300fec9be588bdb90bc7b94ab9e21ddfd0 /packages/web-util/src/utils
parent958747bd08172ce00bc84ea3c543eac414a1d39b (diff)
downloadwallet-core-cfe7129c4ecaa3057d5e47040f0a9efd8ed0317e.tar.xz
adding unreadable http response case and removing deprecated fields
Diffstat (limited to 'packages/web-util/src/utils')
-rw-r--r--packages/web-util/src/utils/request.ts118
1 files changed, 35 insertions, 83 deletions
diff --git a/packages/web-util/src/utils/request.ts b/packages/web-util/src/utils/request.ts
index d05a4ec84..8c77814f7 100644
--- a/packages/web-util/src/utils/request.ts
+++ b/packages/web-util/src/utils/request.ts
@@ -20,6 +20,7 @@ import { base64encode } from "./base64.js";
export enum ErrorType {
CLIENT,
SERVER,
+ UNREADABLE,
TIMEOUT,
UNEXPECTED,
}
@@ -97,12 +98,9 @@ export async function defaultRequestHandler<T>(
url: _url.href,
hasToken: !!options.token,
status: 0,
+ options,
};
const error: HttpRequestTimeoutError = {
- clientError: true,
- isNotfound: false,
- isUnauthorized: false,
- error: undefined,
info,
type: ErrorType.TIMEOUT,
message: "Request timeout",
@@ -124,6 +122,7 @@ export async function defaultRequestHandler<T>(
_url.href,
payload,
!!options.token,
+ options,
);
return result;
} else {
@@ -132,6 +131,7 @@ export async function defaultRequestHandler<T>(
_url.href,
payload,
!!options.token,
+ options,
);
throw new RequestError(error);
}
@@ -152,6 +152,7 @@ export interface RequestInfo {
hasToken: boolean;
payload: any;
status: number;
+ options: RequestOptions;
}
interface HttpResponseLoading<T> {
@@ -185,103 +186,60 @@ export type HttpError<ErrorDetail> =
| HttpRequestTimeoutError
| HttpResponseClientError<ErrorDetail>
| HttpResponseServerError<ErrorDetail>
+ | HttpResponseUnreadableError
| HttpResponseUnexpectedError;
export interface HttpResponseServerError<ErrorDetail> {
ok?: false;
loading?: false;
- /**
- * @deprecated use status
- */
- clientError?: false;
- /**
- * @deprecated use status
- */
- serverError: true;
type: ErrorType.SERVER;
- /**
- * @deprecated use payload
- */
- error: ErrorDetail;
payload: ErrorDetail;
status: HttpStatusCode;
message: string;
- info?: RequestInfo;
+ info: RequestInfo;
}
interface HttpRequestTimeoutError {
ok?: false;
loading?: false;
- /**
- * @deprecated use type
- */
- clientError: true;
- /**
- * @deprecated use type
- */
- serverError?: false;
type: ErrorType.TIMEOUT;
- info?: RequestInfo;
- error: undefined;
+ info: RequestInfo;
- isUnauthorized: false;
- isNotfound: false;
message: string;
}
interface HttpResponseClientError<ErrorDetail> {
ok?: false;
loading?: false;
- /**
- * @deprecated use type
- */
- clientError: true;
- /**
- * @deprecated use type
- */
- serverError?: false;
type: ErrorType.CLIENT;
- info?: RequestInfo;
- /**
- * @deprecated use status
- */
- isUnauthorized: boolean;
- /**
- * @deprecated use status
- */
- isNotfound: boolean;
+ info: RequestInfo;
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;
+ loading: false;
type: ErrorType.UNEXPECTED;
- info?: RequestInfo;
+ info: RequestInfo;
status?: HttpStatusCode;
- /**
- * @deprecated use exception
- */
- error: unknown;
exception: unknown;
message: string;
}
+interface HttpResponseUnreadableError {
+ ok?: false;
+ loading: false;
+ type: ErrorType.UNREADABLE;
+
+ info: RequestInfo;
+ status: HttpStatusCode;
+ exception: unknown;
+ body: string;
+ message: string;
+}
export class RequestError<ErrorDetail> extends Error {
/**
* @deprecated use cause
@@ -317,6 +275,7 @@ async function buildRequestOk<T>(
url: string,
payload: any,
hasToken: boolean,
+ options: RequestOptions,
): Promise<HttpResponseOk<T>> {
const dataTxt = await response.text();
const data = dataTxt ? JSON.parse(dataTxt) : undefined;
@@ -327,6 +286,7 @@ async function buildRequestOk<T>(
payload,
url,
hasToken,
+ options,
status: response.status,
},
};
@@ -337,9 +297,11 @@ async function buildRequestFailed<ErrorDetail>(
url: string,
payload: any,
hasToken: boolean,
+ options: RequestOptions,
): Promise<
| HttpResponseClientError<ErrorDetail>
| HttpResponseServerError<ErrorDetail>
+ | HttpResponseUnreadableError
| HttpResponseUnexpectedError
> {
const status = response?.status;
@@ -348,62 +310,52 @@ async function buildRequestFailed<ErrorDetail>(
payload,
url,
hasToken,
+ options,
status: status || 0,
};
+ const dataTxt = await response.text();
try {
- const dataTxt = await response.text();
const data = dataTxt ? JSON.parse(dataTxt) : undefined;
if (status && status >= 400 && status < 500) {
const error: HttpResponseClientError<ErrorDetail> = {
- clientError: true,
- isNotfound: status === 404,
- isUnauthorized: status === 401,
type: ErrorType.CLIENT,
status,
info,
message: data?.hint,
- error: data, // remove this
payload: data,
};
return error;
}
if (status && status >= 500 && status < 600) {
const error: HttpResponseServerError<ErrorDetail> = {
- serverError: true,
type: ErrorType.SERVER,
status,
info,
message: `${data?.hint} (code ${data?.code})`,
- error: data, //remove this
payload: data,
};
return error;
}
return {
info,
+ loading: false,
type: ErrorType.UNEXPECTED,
status,
- error: {}, // remove this
exception: undefined,
- message: "NOT DEFINED",
+ message: `http status code not handled: ${status}`,
};
} catch (ex) {
- const error: HttpResponseUnexpectedError = {
+ const error: HttpResponseUnreadableError = {
info,
+ loading: false,
status,
- type: ErrorType.UNEXPECTED,
- error: ex,
+ type: ErrorType.UNREADABLE,
exception: ex,
- message: "NOT DEFINED",
+ body: dataTxt,
+ message: "Could not parse body as json",
};
return error;
}
}
-
-// export function isAxiosError<T>(
-// error: AxiosError | any,
-// ): error is AxiosError<T> {
-// return error && error.isAxiosError;
-// }