From 1bd75619dd549625ebcd06848e85716e96051224 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 16 May 2021 12:59:52 +0900 Subject: [PATCH] provide api error detection function --- src/api.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/api.ts b/src/api.ts index 103aa5d..3399d57 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,5 +1,7 @@ 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; }; +export function isAPIError(reason: any): reason is APIError { + return reason[MK_API_ERROR] === true; +} + export function request( origin: string, endpoint: E, @@ -32,7 +38,10 @@ export function request( } else if (res.status === 204) { resolve(null); } else { - reject(body.error); + reject({ + [MK_API_ERROR]: true, + ...body.error + }); } }).catch(reject); });