oliyh / martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
MIT License
529 stars 44 forks source link

spec file with 404 response defined as json gets exception from clj-http #163

Closed michaelwhitford closed 1 year ago

michaelwhitford commented 1 year ago

I'm using clj-http with martian, and have an api that uses 404 with a json body to indicate a resource is not found.

I have a minimum repro for this in a git repo:

https://github.com/michaelwhitford/fulcro-swapi-client

The following line illustrates the problem:

https://github.com/michaelwhitford/fulcro-swapi-client/blob/ca5347644f9545e24ce1be1f00d00054897d0d7a/src/main/swapi/components/apis.clj#L26

When I try to execute a response for this api with a non-existant id, I get an exception from clj-http, rather than the json response object I have in the openapi spec file.

(martian/response-for swapi :planet {:id "40003"}) => Execution error (ExceptionInfo) at slingshot.support/stack-trace (support.clj:201).
                                                      clj-http: status 404

The spec file used for this is in the repository at

https://github.com/michaelwhitford/fulcro-swapi-client/blob/ca5347644f9545e24ce1be1f00d00054897d0d7a/openapi/swapi.yaml#L119

Can you give some pointers on how I would troubleshoot this? I do not see in the martian.clj-http code anywhere that it would override the 404, clj-http has an option to tell it which status codes are exceptions or not, but I it looks like martian is just using all the defaults afaict.

oliyh commented 1 year ago

You are correct, this is default clj-http behaviour.

You can add an interceptor to this route, or all routes, to set the:throw-exceptions false flag in the request, you you can set it in the :impl key in martian options (see readme)

Hope this helps

oliyh commented 1 year ago

Going to close this as the behaviour is explained, please feel free to reopen if you have other suggestions.

Thanks