@@ -139,7 +139,7 @@ There is prepackaged middleware `compojure.api.middleware/api-middleware` for co
139139
140140- catching slingshotted http-errors (` ring.middleware.http-response/catch-response ` )
141141- catching model validation errors (` ring.swagger.middleware/catch-validation-errors ` )
142- - catching unhandler exceptions (` compojure.api.middleware/wrap-exceptions ` )
142+ - catching unhandled exceptions (` compojure.api.middleware/wrap-exceptions ` )
143143- support for different protocols via ` ring.middleware.format-params/wrap-restful-params ` and ` ring.middleware.format-response/wrap-restful-response `
144144 - default supported protocols are: ` :json-kw ` , ` :yaml-kw ` , ` :edn ` , ` :transit-json ` and ` :transit-msgpack `
145145 - enabled protocol support is also published into Swagger docs via ` ring.swagger.middleware/wrap-swagger-data ` .
@@ -433,6 +433,38 @@ the `:components` restucturing with letk-syntax.
433433
434434To see this in action , try `lein run ` and navigate to Components api group.
435435
436+ ## Exception handling
437+
438+ All exceptions should be handled gracefully. Compojure-api ships with customizable exception handling with good
439+ defaults. Customization is done via `api ` options - delegating to `compojure.api.middleware /wrap-exceptions `, which
440+ does the real work. It catches all thrown exceptions and selects a custom handler based on the thrown exception
441+ `ex-data ` value of key `:type `. If an exception doesn 't have ex-data (e.g. legacy Java Exceptions),
442+ `:compojure.api.exception /default ` type is used. Exception handlers are 3 -arity functions , getting the exception ,
443+ ex-data and request as arguments. Below are the default type definitions and default handling:
444+
445+ | type | what | default
446+ |----------------------------------------------- |------------------------------------- |---------
447+ |`:compojure.api.exception /request-parsing ` | Input data de-serialization errors. | 400 + error in body
448+ |`:compojure.api.exception /request-validation ` | Request Schema coercion errors. | 400 + schema error in body
449+ |`:compojure.api.exception /response-validation ` | Response Schema coercion errors. | 500 + schema error in body
450+ |`:compojure.api.exception /default ` | Everything else. | 500 + print stacktrace + safe message
451+
452+ example to override the default case + add a custom exception type + handler for it:
453+
454+ ```clojure
455+ (defn custom-handler [^Exception e data request]
456+ (internal-server-error {:message (.getMessage e)}))
457+
458+ (defn calm-handler [^Exception e data request]
459+ (enhance-your-calm {:message (.getMessage e), :data data}))
460+
461+ (defapi
462+ {:exceptions {:handlers {:compojure.api.exception/default custom-handler
463+ ::calm calm-handler}}}
464+ (GET* " /bang" [] (throw (RuntimeException. " kosh" )))
465+ (GET* " /calm" [] (throw (ex-info " fail" {:type ::calm , :oil " snake" }))))
466+ ```
467+
436468## Schemas
437469
438470Compojure-api uses the [Schema](https://github.com/Prismatic/schema ) to describe data models , backed up by
0 commit comments