Skip to content

Commit 165678a

Browse files
committed
Handle rejection errors
1 parent 212a872 commit 165678a

File tree

4 files changed

+64
-57
lines changed

4 files changed

+64
-57
lines changed

android/lib/src/main/res/raw/trust_min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:23bbfda06906d6d3b9537ce5116b032c9a0ceddd880975e408272b9fe9ff606c
3-
size 683983
2+
oid sha256:b0ac0546df061d79f7a32357747472fc745ccd9e09a29df6f7414008b4c04a26
3+
size 684134

packages/ton/TonBridge.ts

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/ton/types/TonBridge.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export interface TonConnectBridge {
136136
connect(
137137
protocolVersion: number,
138138
message: ConnectRequest,
139-
): Promise<ConnectEvent>;
140-
restoreConnection(): Promise<ConnectEvent>;
139+
): Promise<ConnectEvent | WalletResponseError>;
140+
restoreConnection(): Promise<ConnectEvent | WalletResponseError>;
141141
send(message: AppRequest): Promise<WalletResponse>;
142142
listen(callback: (event: WalletEvent) => void): () => void;
143143
}

0 commit comments

Comments
 (0)