Skip to content
Snippets Groups Projects
Commit 1bd75619 authored by syuilo's avatar syuilo
Browse files

provide api error detection function

parent c7d5da4e
No related branches found
No related tags found
No related merge requests found
import { Endpoints } from './endpoints';
const MK_API_ERROR = Symbol();
export type APIError = {
id: string;
code: string;
......@@ -8,6 +10,10 @@ export type APIError = {
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>(
origin: string,
endpoint: E,
......@@ -32,7 +38,10 @@ export function request<E extends keyof Endpoints>(
} else if (res.status === 204) {
resolve(null);
} else {
reject(body.error);
reject({
[MK_API_ERROR]: true,
...body.error
});
}
}).catch(reject);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment