Error handling

Android SDK has a wrapper called ApiError to help you handle errors within your app. These errors are normally returned back from our RESTful API methods that SDK implements.

When Throwable instance is passed to create ApiError wrapper, you can get following information:

apiError.getThrowable()

Returns original Throwable instance. May be null if ApiError was instantiated with ApiError(Response) constructor.

apiError.printStackTrace()

Prints stack trace on original Throwable instance.

apiError.getHttpCode()

Returns http status code. If request failed to execute (e.g. due to no Internet connection), this value will be equal -1.

apiError.getErrorType()

ErrorType.HTTP_ERROR is returned when request succeeded to execute, but something went wrong and error code is returned (e.g. 403).
ErrorType.NETWORK_ERROR is returned when request failed to execute (e.g. due to no Internet connection).
ErrorType.UNKNOWN is returned when unknown error occurred (e.g. no response form server when expected).

apiError.getHttpErrorCategory()

Returns mapped response’s http code (e.g. 400 http code will be mapped to HttpErrorCategory.BAD_REQUEST or 403 to HttpErrorCategory.FORBIDDEN).

apiError.getErrorBody()

Returns ApiErrorBody parsed from response’s error body. May be null if error type is different than ErrorType.HTTP_ERROR.

Sample usage

When we receive an error, we can check if error is ApiError and then, there is possibility to get more information and get a list of all errors that occurred. Therefore, you can better integrate your own application with the Synerise SDK.

Below sample code shows way to handle API error:

private void showAlertError(ApiError apiError) {
    ApiErrorBody errorBody = apiError.getErrorBody();
    int httpCode = apiError.getHttpCode();
    // create AlertDialog with icon and title
    AlertDialog.Builder dialog = new AlertDialog.Builder(this).setIcon(R.drawable.sygnet_synerise);
    if (httpCode != ApiError.UNKNOWN_CODE) {
        dialog.setTitle(String.valueOf(httpCode));
    } else {
        dialog.setTitle(R.string.default_error);
    }
    // append all available messages from API
    if (errorBody != null) {
        List<ApiErrorCause> errorCauses = errorBody.getErrorCauses();
        StringBuilder message = new StringBuilder(errorBody.getMessage());
        if (!errorCauses.isEmpty())
            for (ApiErrorCause errorCause : errorCauses)
                message.append("\n").append(errorCause.getCode()).append(": ").append(errorCause.getMessage());
        dialog.setMessage(message.toString());
    // if there is no available messages, set default one
    } else {
        switch (apiError.getErrorType()) {
            case HTTP_ERROR:
                if (apiError.getHttpErrorCategory() == UNAUTHORIZED) {
                    dialog.setMessage(getString(R.string.error_unauthorized));
                } else {
                    dialog.setMessage(getString(R.string.error_http));
                }
                break;
            case NETWORK_ERROR:
                dialog.setMessage(getString(R.string.error_network));
                break;
            default:
                dialog.setMessage(getString(R.string.error_default));
        }
    }
    // show dialog
    dialog.show();
}

Obviously that’s just a suggestion how to implement errors exposed by SDK and you can always do it your way.

ApiErrorBody provides attributes like error, message, path, status and list of causes errors.
ApiErrorCause provides attributes like field, code, message, rejectedValue.

😕

We are sorry to hear that

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

😉

Awesome!

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.