Skip to content

Commit

Permalink
Map js client errors (#23)
Browse files Browse the repository at this point in the history
* Map callback error into fine grained errors

* Bump version 1.3.2

* Bump version 2.0.0

* Add changelog
  • Loading branch information
DSergiu authored Mar 24, 2022
1 parent 03cdbfd commit ec5215e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## 2.0.0
- Add more error codes (see readme for full list)
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ You can also customize the look and feel, language, endpoint, etc. by passing a
```java
final HCaptchaConfig config = HCaptchaConfig.builder()
.siteKey(YOUR_API_SITE_KEY)
.apiEndpoint("https://js.hcaptcha.com/1/api.js")
.locale("ro")
.size(HCaptchaSize.INVISIBLE)
.loading(false)
Expand Down Expand Up @@ -101,9 +100,13 @@ HCaptcha.getClient(this).verifyWithHCaptcha(config)...;
In some scenarios in which the human verification process cannot be completed, you should add logic to gracefully handle the errors. The following is a list of possible error codes:

* NETWORK_ERROR (7): there is no internet connection
* INVALID_DATA (8): invalid data is not accepted by endpoints
* CHALLENGE_ERROR (9): js client encountered an error on challenge setup
* INTERNAL_ERROR (10): js client encountered an internal error
* SESSION_TIMEOUT (15): the challenge expired
* CHALLENGE_CLOSED (30): the challenge was closed by the user
* RATE_LIMITED (31): spam detected
* INVALID_CUSTOM_THEME (32): invalid custom theme
* ERROR (29): general failure


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void onClickHCaptcha(final HCaptchaSize hCaptchaSize) {
final String YOUR_API_SITE_KEY = "10000000-ffff-ffff-ffff-000000000001";
final HCaptchaConfig config = HCaptchaConfig.builder()
.siteKey(YOUR_API_SITE_KEY)
.apiEndpoint("https://js.hcaptcha.com/1/api.js")
.size(hCaptchaSize)
.loading(true)
.build();
Expand Down
4 changes: 2 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ android {
// See https://developer.android.com/studio/publish/versioning
// versionCode must be integer and be incremented by one for every new update
// android system uses this to prevent downgrades
versionCode 9
versionCode 10

// version number visible to the user
// should follow semantic versioning (See https://semver.org)
versionName "1.3.1"
versionName "2.0.0"

buildConfigField 'String', 'VERSION_NAME', "\"${defaultConfig.versionName}_${defaultConfig.versionCode}\""

Expand Down
19 changes: 17 additions & 2 deletions sdk/src/main/assets/hcaptcha-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,23 @@
'close-callback': function closeCallback() {
return BridgeObject.onError(30);
},
'error-callback': function errorCallback() {
return BridgeObject.onError(31);
'error-callback': function errorCallback(error) {
switch(error) {
case "rate-limited":
return BridgeObject.onError(31);
case "network-error":
return BridgeObject.onError(7);
case "invalid-data":
return BridgeObject.onError(8);
case "challenge-error":
return BridgeObject.onError(9);
case "internal-error":
return BridgeObject.onError(10);
default:
// Error not handled? Log it for debugging purposes
console.error(error);
return BridgeObject.onError(29);
}
},
'open-callback': function openCallback() {
return BridgeObject.onLoaded();
Expand Down
19 changes: 18 additions & 1 deletion sdk/src/main/java/com/hcaptcha/sdk/HCaptchaError.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


/**
* Enum with all possible hCaptcha errors
* Enum with all possible hCaptcha errors.
* It contains both errors related to the android sdk instance and js client errors.
* More info about js client errors here: https://docs.hcaptcha.com/configuration#error-codes
*/
public enum HCaptchaError implements Serializable {

Expand All @@ -17,6 +19,21 @@ public enum HCaptchaError implements Serializable {
*/
NETWORK_ERROR(7, "No internet connection"),

/**
* Invalid data is not accepted by endpoints.
*/
INVALID_DATA(8, "Invalid data is not accepted by endpoints"),

/**
* User may need to select the checkbox or if invisible programmatically call execute to initiate the challenge again.
*/
CHALLENGE_ERROR(9, "Challenge encountered error on setup"),

/**
* User may need to select the checkbox or if invisible programmatically call execute to initiate the challenge again.
*/
INTERNAL_ERROR(10, "hCaptcha client encountered an internal error"),

/**
* hCaptcha session timed out either the token or challenge expired
*/
Expand Down

0 comments on commit ec5215e

Please sign in to comment.