@@ -67,23 +67,27 @@ export class TonBridge implements TonConnectBridge {
6767 async connect (
6868 protocolVersion : number ,
6969 message : ConnectRequest ,
70- ) : Promise < ConnectEvent > {
71- if ( protocolVersion > this . protocolVersion ) {
72- new TonConnectError ( 'Unsupported protocol version' , 1 ) ;
73- }
70+ ) : Promise < ConnectEvent | WalletResponseError > {
71+ try {
72+ if ( protocolVersion > this . protocolVersion ) {
73+ new TonConnectError ( 'Unsupported protocol version' , 1 ) ;
74+ }
7475
75- const items = await this . provider . send < ConnectItemReply [ ] > (
76- 'tonConnect_connect' ,
77- message ,
78- ) ;
76+ const items = await this . provider . send < ConnectItemReply [ ] > (
77+ 'tonConnect_connect' ,
78+ message ,
79+ ) ;
7980
80- if ( ( items as any ) ?. event === 'connect_error' ) {
81- return this . emit ( items as any ) ;
82- } else {
83- return this . emit ( {
84- event : 'connect' ,
85- payload : { items, device : this . deviceInfo } ,
86- } ) ;
81+ if ( ( items as any ) ?. event === 'connect_error' ) {
82+ return this . emit ( items as any ) ;
83+ } else {
84+ return this . emit ( {
85+ event : 'connect' ,
86+ payload : { items, device : this . deviceInfo } ,
87+ } ) ;
88+ }
89+ } catch ( e ) {
90+ return this . parseError ( e , { id : 0 } ) ;
8791 }
8892 }
8993
@@ -142,7 +146,9 @@ export class TonBridge implements TonConnectBridge {
142146 * @param message
143147 * @returns
144148 */
145- async send ( message : AppRequest ) : Promise < WalletResponse > {
149+ async send (
150+ message : AppRequest ,
151+ ) : Promise < WalletResponse | WalletResponseError > {
146152 try {
147153 const result = await this . provider . send < string > (
148154 `tonConnect_${ message . method } ` ,
@@ -151,42 +157,7 @@ export class TonBridge implements TonConnectBridge {
151157
152158 return { result, id : message . id . toString ( ) } ;
153159 } catch ( e ) {
154- // Parse general RPC rejection errors to ton
155- if ( ( e as any ) ?. code === 4001 ) {
156- return {
157- error : {
158- message : 'User Rejected the transaction' ,
159- code : 300 ,
160- } ,
161- id : String ( message . id ) ,
162- } ;
163- }
164-
165- // If there are too many requests
166- if ( ( e as any ) ?. code === - 32002 ) {
167- return {
168- error : {
169- message : 'Bad request, a transaction is already pending' ,
170- code : 1 ,
171- } ,
172- id : String ( message . id ) ,
173- } ;
174- }
175-
176- // Set default error code if needed
177- if (
178- ( e as WalletResponseError [ 'error' ] ) &&
179- ! [ 0 , 1 , 100 , 300 , 400 ] . includes (
180- ( e as WalletResponseError [ 'error' ] ) . code ,
181- )
182- ) {
183- ( e as WalletResponseError [ 'error' ] ) . code = 0 ;
184- }
185-
186- return {
187- error : e as WalletResponseError [ 'error' ] ,
188- id : String ( message . id ) ,
189- } ;
160+ return this . parseError ( e , { id : message . id } ) ;
190161 }
191162 }
192163
@@ -202,4 +173,40 @@ export class TonBridge implements TonConnectBridge {
202173 this . callbacks = this . callbacks . filter ( ( item ) => item != callback ) ;
203174 } ;
204175 } ;
176+
177+ private parseError ( e : any , message : { id ?: number | string } ) {
178+ if ( ( e as any ) ?. code === 4001 ) {
179+ return {
180+ error : {
181+ message : 'User Rejected the transaction' ,
182+ code : 300 ,
183+ } ,
184+ id : String ( message . id ) ?? 0 ,
185+ } ;
186+ }
187+
188+ // If there are too many requests
189+ if ( ( e as any ) ?. code === - 32002 ) {
190+ return {
191+ error : {
192+ message : 'Bad request, a transaction is already pending' ,
193+ code : 1 ,
194+ } ,
195+ id : String ( message . id ) ?? 0 ,
196+ } ;
197+ }
198+
199+ // Set default error code if needed
200+ if (
201+ ( e as WalletResponseError [ 'error' ] ) &&
202+ ! [ 0 , 1 , 100 , 300 , 400 ] . includes ( ( e as WalletResponseError [ 'error' ] ) . code )
203+ ) {
204+ ( e as WalletResponseError [ 'error' ] ) . code = 0 ;
205+ }
206+
207+ return {
208+ error : e as WalletResponseError [ 'error' ] ,
209+ id : String ( message . id ) ?? 0 ,
210+ } ;
211+ }
205212}
0 commit comments