Skip to content

Commit

Permalink
chore: update cancel code (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
circle-hotaru authored May 16, 2022
1 parent 22e42f3 commit 3dc11b7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions src/apis/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { ERRORS } from '../constants'

export interface StandardError extends Error {
code: number
errorCode?: number // deprecated
}

const isStandardError = <T extends StandardError>(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 => {
Expand Down

0 comments on commit 3dc11b7

Please sign in to comment.