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

Custom error is returned from custom validator and/or decode() function #34

Closed
haemi opened this issue Mar 21, 2022 · 6 comments · Fixed by #37
Closed

Custom error is returned from custom validator and/or decode() function #34

haemi opened this issue Mar 21, 2022 · 6 comments · Fixed by #37
Assignees
Labels
bug Something isn't working
Milestone

Comments

@haemi
Copy link

haemi commented Mar 21, 2022

I have a custom validator like this:

public class ErrorMessageValidator: HTTPValidator {

    public func validate(response: HTTPResponse, forRequest request: HTTPRequest) -> HTTPResponseValidatorResult {
        if [HTTPStatusCode.ResponseType.success, .redirection].contains(response.statusCode.responseType) {
            return .nextValidator
        }

        guard let data = response.data else {
            return .failChain(HTTPError(.invalidResponse))
        }

        let jsonData = JSON(data)

        guard let message = jsonData.dictionary?["message"]?.string else {
            return .nextValidator
        }

        return .failChain(MyError(title: message))
    }
}

And I get to the last line as expected, so a MyError is returned, wrapped into the failChain.

But in my calling code:

let request = myServerCall(myParameters).httpRequest()
let response = try await request.fetch(MyModel.self)

an error is thrown (expected) but it is an HTTPError with an empty error property, so my MyError isn't available here

@malcommac
Copy link
Collaborator

May you post the structure of MyError so I can replicate the error? Thanks!

@malcommac malcommac self-assigned this Mar 22, 2022
@malcommac malcommac changed the title error returned from custom validator not received in calling code? Error returned from custom validator not received in calling code? Mar 22, 2022
@haemi
Copy link
Author

haemi commented Mar 22, 2022

it's very simple struct:

public struct MyError: Error {
    public let title: String

    public init(title: String) {
        self.title = title
    }
}

malcommac added a commit that referenced this issue Mar 22, 2022
malcommac added a commit that referenced this issue Mar 22, 2022
@malcommac
Copy link
Collaborator

Okay I've fixed the dispatch of custom errors. Moreover I decided to dispatch original response of the request and attach additional error information based upon the library activity (max attempts, validation errors etc.).
May you take a look to see if it works for you?
I've also added two dedicated unit tests.

@malcommac malcommac added the bug Something isn't working label Mar 22, 2022
@malcommac malcommac added this to the 1.3.2 milestone Mar 22, 2022
@haemi
Copy link
Author

haemi commented Mar 22, 2022

it works when I use

let response = try await request.fetch()
// now, response.error?.error is MyError

but when I use

let myModel = try await request.fetch(MyModel.self)

the error isn't thrown if the body can be encoded. I would have expected that somehow I get to the error then as well, but there's no way as myModel is of type MyModel. Currently it's only possible to do it in multiple steps:

let response = try await request.fetch()
// check if response.error?.error is set, if not:
let myModel = try response.decode(MyModel.self)

@malcommac
Copy link
Collaborator

You are absolutely right, the fetch error should be passed outside the decode() function.
However the error must be an HTTPError so you would read it as:

do {
  _ = try await req.fetch(client: newClient, DummyDecodeStruct.self)
} catch {
  let customUnderlyingError = (error as? HTTPError)?.error as? MyError
  print(customUnderlyingError)
}

malcommac added a commit that referenced this issue Mar 23, 2022
…dispatch received error from network outside
@malcommac
Copy link
Collaborator

ok I've fixed the code and added the necessary unit test

@malcommac malcommac changed the title Error returned from custom validator not received in calling code? Custom error is returned from custom validator and/or decode() function Mar 23, 2022
malcommac added a commit that referenced this issue Mar 23, 2022
malcommac added a commit that referenced this issue Mar 23, 2022
#34 Fix for custom error dispatch for decode() and throws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants