Skip to content

Parse Swift can't decode custom ParseCloud errors #164

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

Closed
dblythy opened this issue Jun 16, 2021 · 7 comments · Fixed by #165
Closed

Parse Swift can't decode custom ParseCloud errors #164

dblythy opened this issue Jun 16, 2021 · 7 comments · Fixed by #165
Labels
type:feature New feature or improvement of existing feature

Comments

@dblythy
Copy link
Member

dblythy commented Jun 16, 2021

Throwing a custom error code from a Parse Cloud function results in:

ParseError(code: ParseSwift.ParseError.Code.unknownError, message: "Error decoding parse-server response: Optional(<NSHTTPURLResponse: 0x100d74140> { URL: http://localhost:1337/1/jobs/test } { Status Code: 200, Headers {\n    \"Content-Type\" =     (\n        \"application/json\"\n    );\n} }) with error: The data couldn’t be read because it is missing. 

Failing case:

    func testCustomError() {

        let encoded: Data = "{\"error\":\"Error: Custom Error\",\"code\":2000}".data(using: .utf8)!

        MockURLProtocol.mockRequests { _ in
            return MockURLResponse(data: encoded, statusCode: 200, delay: 0.0)
        }
        do {
            let cloud = Cloud(functionJobName: "test")
            _ = try cloud.startJob()
            XCTFail("Should have thrown ParseError")
        } catch {
            if let error = error as? ParseError {
                XCTAssertNotEqual(error.code, ParseError.Code.unknownError)
                XCTAssertEqual(error.code.rawValue, 2000)
            } else {
                XCTFail("Should have thrown ParseError")
            }
        }
    }

Proposed solution:

Add .other enum to error, and intCode property to Parse.Error

@cbaker6
Copy link
Contributor

cbaker6 commented Jun 16, 2021

@dblythy thanks for your PR! Here’s a response I gave to a similar proposal? #106 (comment)

From your addition, does this mean the other SDKs currently allow users to create their own errors and mask them as Parse errors? I could be wrong here, but I would think errors would come from a set list of known Parse errors. If a user generates an error, maybe it can fall under “unknownError” or add a new error code on the server specific to user errors? Not sure what’s the best approach, but definitely open to hear you and the servers team thoughts.

My fear is allowing developers to mask their errors as parse errors and then people submit issues to Parse repos claiming they are Parse errors when they are not

@cbaker6
Copy link
Contributor

cbaker6 commented Jun 16, 2021

My reasoning of using unknownError is because of how Flo documented ParseError before I took over it:

Internal SDK Error. No information available
*/
case unknownError = -1

It seems believed all SDK errors, technically custom SDK errors should fall under this error. So I can see other errors going here as well

@dblythy
Copy link
Member Author

dblythy commented Jun 16, 2021

The main problem is that if you use a custom error, (which is already supported in Parse Server as well as every other SDK) Parse Swift can't decode it.

Such as:

Parse.Cloud.define("runFunction", ({user}) => {
  if (user.get('trialExpired')) {
     throw new Parse.Error(9000, "Your trial has expired");
  }
  ...
});

Call function:

if (e.code === 9000) {
// redirect to upgrade page
}

parse-community/docs#943

@cbaker6
Copy link
Contributor

cbaker6 commented Jun 16, 2021

If I understand the discussion you linked it’s proposing to restrict custom errors to a specific range? parse-community/docs#943

That seems reasonable I guess and prevent some of my concern above

@dblythy
Copy link
Member Author

dblythy commented Jun 16, 2021

The discussion linked is more to highlight that throwing specific custom errors from Cloud Code or third party modules is a common use case.

The main use for me is to change the behaviour of the app based on the custom error code response from the Parse Server.

@dblythy
Copy link
Member Author

dblythy commented Jun 17, 2021

FYI to anyone hoping to use this, you can use ParseError.otherCode if you are throwing custom Parse Errors from Parse Cloud

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants