@@ -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'
1824import config from '../package.json'
1925import { AddressType , getAddressType } from './utils/address'
2026import { 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
0 commit comments