Error Handling

SNRApiError is a superclass for standard iOS class for error handling - NSError.

It’s designed to help you handle errors within your application. It provides details about failures in communication with the Synerise API.

This error is normally returned from the SDK methods that communicate with the Synerise API.

Type

You can check this by calling getType() method.

SNRApiErrorType.Http

Returned when a request is executed, but something else goes wrong and an error code is returned (e.g. 403).

SNRApiErrorType.Network

Returned when a request fails to execute (e.g. due to no Internet connection).

SNRApiErrorType.UnauthorizedSession

It’s returned when session is invalid for given request.

SNRApiErrorType.Unknown

Returned when an unknown error occurs (e.g. no response from server when expected).


HTTP Code

You can check this by calling the getHttpCode() method.

The method returns the HTTP status code. If a request failed to execute (e.g. due to no Internet connection), this value will be equal to -1.


Body

You can check this by calling the getBody() method.

Returns a description parsed from the response’s error cause list.

Note: May be null if the error type is different than SNRApiErrorType.Http.

Errors

You can check this by getting the errors property.

Returns a list of standard NSError objects that describe failure causes from the Synerise API.

Custom attributes provided in userInfo property:

Attribute User Info key Description
CODE code Code corresponding to the error that has occurred from the Synerise API
FIELD field Name of the argument that didn’t pass validation
PATH path Path of the argument that didn’t pass validation
MESSAGE message Description of the error
REJECTED VALUE rejectedValue Value that was rejected

Example

When you receive an error from the SDK method, you can check if the error is an SNRApiError instance and then you can work with the properties described above.

func presentAlert(title: String, message: String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

    let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertController.addAction(okAction)

    self.present(alertController, animated: true, completion: nil)
}

func showErrorInfo(_ error: NSError, title: String = "Error", debug: Bool = true) {
    if let apiError = error as? SNRApiError {
        var apiErrorDebugInfo: String = String()

        let apiErrorType: SNRApiErrorType = apiError.getType()

        switch (apiErrorType) {
        case .network: apiErrorDebugInfo.append("NETWORK ERROR")
        case .unauthorizedSession: apiErrorDebugInfo.append("UNAUTHORIZED SESSION ERROR")
        case .http: apiErrorDebugInfo.append("HTTP ERROR: \(apiError.getHttpCode())")
        case .unknown: apiErrorDebugInfo.append("UNKNOWN ERROR")
        }

        apiErrorDebugInfo.append("\n\n")
        apiErrorDebugInfo.append("\(apiError.localizedDescription)")

        // first approach
        if let apiErrorCauses = apiError.errors, !apiErrorCauses.isEmpty {
            apiErrorDebugInfo.append("\n\n")

            apiErrorCauses.forEach({ (error) in
                let apiErrorCause: NSError = error as NSError

                var apiErrorCauseString: String = String()
                apiErrorCauseString.append("CODE: \(apiErrorCause.code)")
                apiErrorCauseString.append("\n")
                apiErrorCauseString.append("MESSAGE: \(apiErrorCause.localizedDescription)")

                apiErrorDebugInfo.append(apiErrorCauseString)
                apiErrorDebugInfo.append("\n\n")
            })
        }

        // second approach
        // apiErrorDebugInfo.append("\n\n")
        //
        // let apiErrorCauseString: String = apiError.getBody() ?? ""
        // apiErrorDebugInfo.append(apiErrorCauseString)

        self.presentAlert(title: "Debug SNRApiError", message: apiErrorDebugInfo)

        if debug {
            DebugUtils.print("\(title) \(apiError.code) \(apiError.localizedDescription)")
        }

        return
    }

    if debug {
        DebugUtils.print("\(title) \(error.code) \(error.localizedDescription)")
    }
}

func signIn(email: String, password: String) {
    Client.signIn(email: email, password: password, deviceId: nil, success: { (success) in
        //...
    }, failure: { (error) in
        self.showErrorInfo(error as NSError)
    })
}

😕

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.