immobiliare/RealHTTP

Why custom validation errors are embedded in a HTTPError ?

nashfive opened this issue ยท 3 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 ;)

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

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

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