refactor: throw a custom S5Error
This commit is contained in:
parent
dfe79e2fc2
commit
4561e06b61
12
src/axios.ts
12
src/axios.ts
|
@ -1,4 +1,5 @@
|
||||||
import Axios, { AxiosRequestConfig } from "axios";
|
import Axios, {AxiosError, AxiosRequestConfig} from "axios";
|
||||||
|
import {S5Error} from "#client.js";
|
||||||
|
|
||||||
export const customInstance = <T>(
|
export const customInstance = <T>(
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
|
@ -15,12 +16,17 @@ export const customInstance = <T>(
|
||||||
delete config.data;
|
delete config.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance = Axios.create({ baseURL: options?.baseURL });
|
const instance = Axios.create({baseURL: options?.baseURL});
|
||||||
const promise = instance({
|
const promise = instance({
|
||||||
...config,
|
...config,
|
||||||
...options,
|
...options,
|
||||||
cancelToken: source.token,
|
cancelToken: source.token,
|
||||||
}).then(({ data }) => data);
|
}).then(({data}) => data).catch((error) => {
|
||||||
|
if (Axios.isCancel(error)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new S5Error((error as AxiosError).message, (error as AxiosError).response?.status as number);
|
||||||
|
})
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
promise.cancel = () => {
|
promise.cancel = () => {
|
||||||
|
|
|
@ -70,6 +70,16 @@ import { Multihash } from "@lumeweb/libs5/lib/multihash.js";
|
||||||
import { blake3 } from "@noble/hashes/blake3";
|
import { blake3 } from "@noble/hashes/blake3";
|
||||||
import { base64urlDecode, base64urlEncode } from "#utils/encoding.js";
|
import { base64urlDecode, base64urlEncode } from "#utils/encoding.js";
|
||||||
|
|
||||||
|
export class S5Error extends Error {
|
||||||
|
public statusCode: number;
|
||||||
|
|
||||||
|
constructor(message: string, statusCode: number) {
|
||||||
|
super(message);
|
||||||
|
this.name = "S5Error";
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The S5 Client which can be used to access S5-net.
|
* The S5 Client which can be used to access S5-net.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue