Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper error code and message #347

Merged
merged 2 commits into from
Jul 25, 2023
Merged

Use proper error code and message #347

merged 2 commits into from
Jul 25, 2023

Conversation

roeierez
Copy link
Member

Propagating the error code and message to the errors thrown from the native layer.
To make it generic reflection is used in both platform to extract the class name, e.g:
SdkError.Generic => Generic

@roeierez roeierez requested a review from dangeross July 23, 2023 08:24
Copy link
Collaborator

@dangeross dangeross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better with the code set to the enum value, but there is an inconsistency in the message between iOS and android. The iOS error message for example is still wrapped with InitFailed(message: \"...\") where on android it is not.

iOS:

{
  "code": "InitFailed",
  "message": "InitFailed(message: \"Failed to initialize the SDK: Failed to connect to Greenlight: Failed to decrypt credentials, seed doesn\\'t match existing node\")",
  "domain": "BreezSDK.SdkError",
  "userInfo": {},
  "nativeStackIOS": [ ... ]
}

android:

{
  "nativeStackAndroid": [ ... ],
  "userInfo": null,
  "message": "Failed to initialize the SDK: Failed to connect to Greenlight: status: Internal, message: \"Unable to register node: not authorized: an invite code or a partner certificate is require to register a new node (see https://bit.ly/glinvites for details\", details: [], metadata: MetadataMap { headers: {\"content-type\": \"application/grpc\", \"date\": \"Sun, 23 Jul 2023 18:26:40 GMT\", \"content-length\": \"0\"} }",
  "code": "InitFailed",
  "line": 2527,
  "column": 45,
  "sourceURL": "http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false&app=com.example.breezsdk&modulesOnly=false&runModule=true"
}

the best way I could come up with to solve this is changing the rejectErr func to also reflect the enum's associated variables:

    func rejectErr(err: Error, reject: @escaping RCTPromiseRejectBlock) {
        var errorCode = "Generic"
        var message = "\(err)"
        if let sdkErr = err as? SdkError {
            if let sdkErrAssociated = Mirror(reflecting: sdkErr).children.first {
                if let associatedMessage = Mirror(reflecting: sdkErrAssociated.value).children.first {
                    message = associatedMessage.value as! String
                }
            }
        }
        reject(errorCode, message, err)
    }

which results in:

{
  "code": "Generic",
  "message": "Failed to initialize the SDK: Failed to connect to Greenlight: status: Internal, message: \"Unable to register node: not authorized: an invite code or a partner certificate is require to register a new node (see https://bit.ly/glinvites for details\", details: [], metadata: MetadataMap { headers: {\"content-type\": \"application/grpc\", \"date\": \"Sun, 23 Jul 2023 20:07:25 GMT\", \"content-length\": \"0\"} }",
  "domain": "BreezSDK.SdkError",
  "userInfo": {},
  "nativeStackIOS": [ ... ]
}

@roeierez roeierez merged commit 4c6b001 into main Jul 25, 2023
1 of 2 checks passed
@erdemyerebasmaz erdemyerebasmaz deleted the rn-error-handling branch December 7, 2023 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants