Skip to content

Commit 3deb99b

Browse files
Add error callback (#27)
1 parent 1d132cf commit 3deb99b

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ These options are well documented in the [Cloudflare Docs](https://developers.cl
8989
### Events
9090

9191
- `resolved(response: string)`. Occurs when the CAPTCHA resolution value changed.
92+
- `errored(errorCode: string)`. Occurs when the CAPTCHA widget returned an [error code](https://developers.cloudflare.com/turnstile/troubleshooting/client-side-errors/#error-codes).
9293

9394
### Example
9495

projects/ngx-turnstile-demo/src/app/examples/event-binding/event-binding-example.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { NgxTurnstileModule } from 'ngx-turnstile';
1010
[siteKey]="siteKey"
1111
theme="light"
1212
(resolved)="onResolved($event)"
13+
(errored)="onErrored($event)"
1314
></ngx-turnstile>
1415
</ng-container>
1516
`,
@@ -19,6 +20,10 @@ export class EventBindingExampleComponent {
1920
siteKey = '1x00000000000000000000AA';
2021

2122
onResolved(response: string | null) {
22-
console.log(response);
23+
console.log('onResolved', response);
24+
}
25+
26+
onErrored(errorCode: string | null) {
27+
console.log('onErrored', errorCode);
2328
}
2429
}

projects/ngx-turnstile/src/lib/interfaces/turnstile-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface TurnstileOptions {
33
action?: string;
44
cData?: string;
55
callback?: (token: string) => void;
6-
'error-callback'?: () => void;
6+
'error-callback'?: (errorCode: string) => boolean;
77
'expired-callback'?: () => void;
88
theme?: 'light' | 'dark' | 'auto';
99
tabindex?: number;

projects/ngx-turnstile/src/lib/ngx-turnstile.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class NgxTurnstileComponent implements AfterViewInit, OnDestroy {
4545
@Input() appearance?: 'always' | 'execute' | 'interaction-only' = 'always';
4646

4747
@Output() resolved = new EventEmitter<string | null>();
48+
@Output() errored = new EventEmitter<string | null>();
4849

4950
private widgetId!: string;
5051

@@ -72,6 +73,11 @@ export class NgxTurnstileComponent implements AfterViewInit, OnDestroy {
7273
callback: (token: string) => {
7374
this.zone.run(() => this.resolved.emit(token));
7475
},
76+
'error-callback': (errorCode: string): boolean => {
77+
this.zone.run(() => this.errored.emit(errorCode));
78+
// Returning false causes Turnstile to log error code as a console warning.
79+
return false;
80+
},
7581
'expired-callback': () => {
7682
this.zone.run(() => this.reset());
7783
},

0 commit comments

Comments
 (0)