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

Why custom validation errors are embedded in a HTTPError ? #53

Closed
nashfive opened this issue Jun 14, 2022 · 3 comments
Closed

Why custom validation errors are embedded in a HTTPError ? #53

nashfive opened this issue Jun 14, 2022 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@nashfive
Copy link

I have a custom validator that parses the JSON response and tries to build an Error struct.
If it builds the error successfully, my validator will return with a .failChain(responseError), but in my catch at the call site, the error is still embedded into a HTTPError, which makes the catch a bit convoluted:

import RealHTTP
import MyService

do { 
  let result = try await service.callSomeEndpoint()
} catch let httpError as HTTPError {
  if let responseError = httpError.error as? ResponseError {
    // handle my custom response error here
  } else {
    // handle the http error?
  }
} catch {
  // 
}

Question: what's your rationale behind automatically nesting the validator custom errors in a HTTPError ?

I would probably prefer to have 2 dedicated catches:

} catch let responseError as ResponseError {
  ...
} catch let httpError as HTTPError {
  ...
}

So I can ignore the HTTPError and fallback to a generic catch if I'd like to.. and I could also avoid leaking the RealHTTP implementation/imports in all my services ;)

@nashfive nashfive changed the title Why my custom validation error is embedded in an HTTPError ? Why custom validation errors are embedded in a HTTPError ? Jun 14, 2022
@malcommac
Copy link
Collaborator

Hi, I agree with your point. Would you make a PR to support this enhancement

@malcommac malcommac added enhancement New feature or request good first issue Good for newcomers labels Jul 22, 2022
@nashfive
Copy link
Author

@malcommac ok, I'll try to find the time for that in a near future

@nashfive
Copy link
Author

Ok, I gave it a try, but changing the error type HTTPError to Error? brings a lot of changes and I am not so sure about the necessity of it anymore 😅
So to isolate things a bit, I now have my custom client implementation catching any errors thrown by HTTPClient and throwing only one type of Error that my custom one...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants