From 3dc11b780b638d32b9cb743bade93c70cc3da7d7 Mon Sep 17 00:00:00 2001 From: circlehotarux Date: Mon, 16 May 2022 11:05:42 +0800 Subject: [PATCH] chore: update cancel code (#45) --- package.json | 2 +- src/__tests__/utils.test.ts | 2 +- src/apis/utils.ts | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index fd5e37d..050f066 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@consenlabs-fe/webview", - "version": "0.1.0", + "version": "0.1.1", "description": "", "main": "dist/index.js", "module": "dist/index.es.js", diff --git a/src/__tests__/utils.test.ts b/src/__tests__/utils.test.ts index d92bf24..abc33cf 100644 --- a/src/__tests__/utils.test.ts +++ b/src/__tests__/utils.test.ts @@ -45,7 +45,7 @@ describe('utils', () => { expect(TokenWebView.isCancelError(new Error(TokenWebView.ERRORS.USER_CANCEL))).toBe(true) const nonstandardError = { - errorCode: 1001, + code: 4001, } const isError = TokenWebView.isCancelError as any expect(isError(nonstandardError)).toBe(true) diff --git a/src/apis/utils.ts b/src/apis/utils.ts index 2b05f5a..e304b9a 100644 --- a/src/apis/utils.ts +++ b/src/apis/utils.ts @@ -1,15 +1,24 @@ import { ERRORS } from '../constants' +export interface StandardError extends Error { + code: number + errorCode?: number // deprecated +} + +const isStandardError = (error: T): boolean => error.code !== undefined + /** * Identify if the error was cancelled by the user himself. */ -export const isCancelError = (error: Error | string) => { +export const isCancelError = (error: StandardError | string) => { if (typeof error === 'string') { return error === ERRORS.USER_CANCEL } if (error.message === ERRORS.USER_CANCEL) return true - return (error as any).errorCode === 1001 + + const isCancel = (error: StandardError) => isStandardError(error) ? error.code === 4001 : error?.errorCode === 1001 + return isCancel(error) } export const isTokenWebView = (): boolean => {