Skip to content

Commit 51ad3f6

Browse files
committed
added error states
1 parent dfbb707 commit 51ad3f6

4 files changed

Lines changed: 68 additions & 10 deletions

File tree

packages/elements/example/index.html

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,9 @@
3838

3939
const { button } = code.elements.create('button', options);
4040

41-
button.on('error', () => { console.log('sdk-error'); });
41+
button.on('error', (e) => { console.log('sdk-error', e); });
4242
button.on('cancel', (e) => { console.log('sdk-cancel', e); });
43-
button.on('success', (e) => { console.log('sdk-success', e); alert('thank you'); });
44-
45-
/*
46-
button.on('invoke', async () => {
47-
await sleep(2000);
48-
button.update({ idempotencyKey: '123xyz'+Math.random() });
49-
});
50-
*/
43+
button.on('success', (e) => { console.log('sdk-success', e); alert('thank you'); return true; });
5144

5245
button.mount(id);
5346
}

packages/elements/src/components/elements/PaymentRequestModalDesktop.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
'clientRejectedPayment',
1313
'intentSubmitted',
1414
'channelCreated',
15+
'error',
16+
'streamTimeout',
17+
'streamClosed',
1518
]);
1619
1720
const channel = new EventChannel<InternalEvents>();
@@ -30,6 +33,25 @@
3033
emit('intentSubmitted');
3134
});
3235
36+
// Error states
37+
channel.on('error' , (err) => {
38+
if (`${err}`.includes('NGHTTP2_INTERNAL_ERROR')) {
39+
console.warn('CodeSDK: GRPC stream closed.');
40+
emit(`streamClosed`);
41+
} else {
42+
console.error(`CodeSDK: ${err}`);
43+
emit('error', err);
44+
}
45+
});
46+
channel.on('streamTimeout' , () => {
47+
console.warn('CodeSDK: Websocket Stream timeout.');
48+
emit('streamTimeout');
49+
});
50+
channel.on('streamClosed' , () => {
51+
console.warn('CodeSDK: Websocket Stream closed.');
52+
emit('streamClosed');
53+
});
54+
3355
onMounted(() => {
3456
if (!el.value) { return; }
3557

packages/elements/src/components/elements/PaymentRequestModalMobile.vue

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
'codeScanned',
1212
'clientRejectedPayment',
1313
'intentSubmitted',
14-
'channelCreated'
14+
'channelCreated',
15+
'error',
16+
'streamTimeout',
17+
'streamClosed',
1518
]);
1619
1720
const channel = new EventChannel<InternalEvents>();
@@ -35,6 +38,25 @@
3538
window.location.href = event.url;
3639
});
3740
41+
// Error states
42+
channel.on('error' , (err) => {
43+
if (`${err}`.includes('NGHTTP2_INTERNAL_ERROR')) {
44+
console.warn('CodeSDK: GRPC stream closed.');
45+
emit(`streamClosed`);
46+
} else {
47+
console.error(`CodeSDK: ${err}`);
48+
emit('error', err);
49+
}
50+
});
51+
channel.on('streamTimeout' , () => {
52+
console.warn('CodeSDK: Websocket Stream timeout.');
53+
emit('streamTimeout');
54+
});
55+
channel.on('streamClosed' , () => {
56+
console.warn('CodeSDK: Websocket Stream closed.');
57+
emit('streamClosed');
58+
});
59+
3860
onMounted(() => {
3961
if (!el.value) { return; }
4062

packages/elements/src/components/flows/ButtonFlow.vue

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ async function onCancel() {
121121
}
122122
}
123123
124+
async function onError(err: any) {
125+
// Let the user know that something went wrong
126+
await sdkEmit('error', { message: `${err}` });
127+
}
128+
129+
async function onStreamTimeout() {
130+
open.value = false;
131+
await onError('stream_timed_out');
132+
}
133+
134+
async function onStreamClosed() {
135+
open.value = false;
136+
await onError('stream_closed');
137+
}
138+
124139
// Add an event listener for the 'visibilitychange' event
125140
document.addEventListener("visibilitychange", async () => {
126141
if (document.visibilityState === "visible") {
@@ -149,13 +164,19 @@ document.addEventListener("visibilitychange", async () => {
149164
@channel-created="onChannelCreated"
150165
@intent-submitted="onSuccess"
151166
@client-rejected-payment="onCancel"
167+
@error="onError"
168+
@stream-timeout="onStreamTimeout"
169+
@stream-closed="onStreamClosed"
152170
/>
153171

154172
<PaymentRequestModalDesktop
155173
v-else
156174
@channel-created="onChannelCreated"
157175
@intent-submitted="onSuccess"
158176
@client-rejected-payment="onCancel"
177+
@error="onError"
178+
@stream-timeout="onStreamTimeout"
179+
@stream-closed="onStreamClosed"
159180
/>
160181
</div>
161182
<div v-else>

0 commit comments

Comments
 (0)