RealHTTP icon indicating copy to clipboard operation
RealHTTP copied to clipboard

Why custom validation errors are embedded in a HTTPError ?

Open nashfive opened this issue 3 years ago • 2 comments

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 avatar Jun 14 '22 10:06 nashfive

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

malcommac avatar Jul 22 '22 17:07 malcommac

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

nashfive avatar Jul 28 '22 07:07 nashfive

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...

nashfive avatar Aug 23 '22 09:08 nashfive