Skip to content

Commit 11760fb

Browse files
committed
fix(error standard): throw error standard
1 parent 9325356 commit 11760fb

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@idealight-labs/anyweb-js-sdk",
33
"description": "AnyWeb JavaScript Software Development Kit",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"license": "LGPL-3.0",
66
"author": "common@idealight.ltd",
77
"repository": {

src/error/provider.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/interface/provider.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ export interface IBaseProviderOptions {
1818
appId: string
1919
}
2020

21-
/**
22-
* Provider RPC Error
23-
*/
24-
export interface IProviderRpcError extends Error {
25-
message: string
26-
code: number
27-
data?: unknown
28-
}
29-
3021
/**
3122
* Base Provider
3223
*/

src/provider.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import {
1414
IProviderRpcError,
1515
IRequestArguments,
1616
} from './interface/provider'
17-
import { callIframe, createIframe, isObject } from './utils/common'
17+
import {
18+
callIframe,
19+
createIframe,
20+
isObject,
21+
ProviderErrorCode,
22+
ProviderRpcError,
23+
} from './utils/common'
1824
import config from '../package.json'
1925
import { AddressType, getAddressType } from './utils/address'
2026
import { ConsoleLike } from './utils/types'
@@ -99,7 +105,10 @@ export class Provider implements IProvider {
99105
if (params) {
100106
Provider.instance = new Provider(params)
101107
} else {
102-
throw new Error('[AnyWeb] Provider is not initialized')
108+
throw new ProviderRpcError(
109+
ProviderErrorCode.SDKNotReady,
110+
'Provider is not initialized'
111+
)
103112
}
104113
}
105114
return Provider.instance
@@ -114,7 +123,10 @@ export class Provider implements IProvider {
114123
if (arg.length > 1) {
115124
return await this.request({ method: arg[0], params: arg[1] })
116125
}
117-
throw new Error('Invalid arguments')
126+
throw new ProviderRpcError(
127+
ProviderErrorCode.ParamsError,
128+
'Invalid arguments'
129+
)
118130
}
119131

120132
/**
@@ -139,12 +151,19 @@ export class Provider implements IProvider {
139151
*/
140152
async request(args: IRequestArguments): Promise<unknown> {
141153
if (!args || typeof args !== 'object' || Array.isArray(args)) {
142-
throw new Error('Invalid request arguments')
154+
throw new ProviderRpcError(
155+
ProviderErrorCode.ParamsError,
156+
'Invalid request arguments'
157+
)
143158
}
144159
const { method, params } = args
145160
if (!method || method.trim().length === 0) {
146-
throw new Error('Method is required')
161+
throw new ProviderRpcError(
162+
ProviderErrorCode.ParamsError,
163+
'Invalid request arguments: Method is required'
164+
)
147165
}
166+
148167
console.debug(`[AnyWeb] request ${method} with`, params)
149168
const result = await this.rawRequest(method, params)
150169
console.debug(`[AnyWeb] request(${method}):`, result)
@@ -168,8 +187,9 @@ export class Provider implements IProvider {
168187
*/
169188
protected async rawRequest(method: string, params?: any): Promise<unknown> {
170189
if (!Provider.ready) {
171-
throw new Error(
172-
"[AnyWeb] Provider is not ready, please use on('ready', callback) to listen to ready event"
190+
throw new ProviderRpcError(
191+
ProviderErrorCode.SDKNotReady,
192+
"Provider is not ready, please use on('ready', callback) to listen to ready event"
173193
)
174194
}
175195
switch (method) {
@@ -361,7 +381,10 @@ export class Provider implements IProvider {
361381
return false
362382
}
363383
default:
364-
return 'Unsupported method'
384+
throw new ProviderRpcError(
385+
ProviderErrorCode.UnsupportedMethod,
386+
'Unsupported Method: ' + method
387+
)
365388
}
366389
}
367390

src/utils/common.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
IIframeData,
99
IIframeEventData,
1010
IIframeOptions,
11+
IProviderRpcError,
1112
} from '../interface/provider'
1213
import { Provider } from '../provider'
1314

@@ -197,6 +198,11 @@ export const createIframe = async (url: string) => {
197198

198199
button.onclick = () => {
199200
closeIframe(mask)
201+
202+
throw new ProviderRpcError(
203+
ProviderErrorCode.Unauthorized,
204+
'User canceled the operation'
205+
)
200206
}
201207
document.body.appendChild(style)
202208

@@ -352,3 +358,25 @@ export const isArrEqual = <T>(arr1: T[], arr2: T[]) => {
352358
export const isIncluded = <T>(arr1: T[], arr2: T[]): boolean => {
353359
return arr1.length === new Set([...arr1, ...arr2]).size
354360
}
361+
362+
export enum ProviderErrorCode {
363+
UserRejectedRequest = 4001,
364+
Unauthorized = 4100,
365+
UnsupportedMethod = 4200,
366+
Disconnected = 4900,
367+
ChainDisconnected = 4901,
368+
SDKNotReady = 5000,
369+
ParamsError = 6000,
370+
}
371+
372+
export class ProviderRpcError extends Error implements IProviderRpcError {
373+
code: number
374+
data?: unknown
375+
376+
constructor(code: ProviderErrorCode, message: string) {
377+
super('[AnyWeb] ' + message)
378+
console.debug(`[AnyWeb] Throw the error(${code}):` + message)
379+
this.code = code
380+
this.name = ProviderErrorCode[code]
381+
}
382+
}

0 commit comments

Comments
 (0)