mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-13 06:23:08 +02:00
provide api error detection function
This commit is contained in:
parent
c7d5da4ec5
commit
1bd75619dd
1 changed files with 10 additions and 1 deletions
11
src/api.ts
11
src/api.ts
|
@ -1,5 +1,7 @@
|
||||||
import { Endpoints } from './endpoints';
|
import { Endpoints } from './endpoints';
|
||||||
|
|
||||||
|
const MK_API_ERROR = Symbol();
|
||||||
|
|
||||||
export type APIError = {
|
export type APIError = {
|
||||||
id: string;
|
id: string;
|
||||||
code: string;
|
code: string;
|
||||||
|
@ -8,6 +10,10 @@ export type APIError = {
|
||||||
info: Record<string, any>;
|
info: Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function isAPIError(reason: any): reason is APIError {
|
||||||
|
return reason[MK_API_ERROR] === true;
|
||||||
|
}
|
||||||
|
|
||||||
export function request<E extends keyof Endpoints>(
|
export function request<E extends keyof Endpoints>(
|
||||||
origin: string,
|
origin: string,
|
||||||
endpoint: E,
|
endpoint: E,
|
||||||
|
@ -32,7 +38,10 @@ export function request<E extends keyof Endpoints>(
|
||||||
} else if (res.status === 204) {
|
} else if (res.status === 204) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
} else {
|
} else {
|
||||||
reject(body.error);
|
reject({
|
||||||
|
[MK_API_ERROR]: true,
|
||||||
|
...body.error
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue