Skip to content

Commit 6d83d4a

Browse files
committed
Merge branch 'master' into ordering
2 parents 75ec46b + fdbbae6 commit 6d83d4a

4 files changed

Lines changed: 46 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
## 0.23.0-SNAPSHOT
1+
## 0.23.0 (1.9.2016)
22

33
* Ring-swagger 0.21.0
44
* **BREAKING**: new signature for dispatching custom JSON Schema transformations, old signature will break (nicely at compile-time), see [Readme](https://github.com/metosin/ring-swagger/blob/master/README.md) for details.
55
* Support for collections in query parameters. E.g. `:query-params [x :- [Long]]` & url `?x=1&x=2&x=3` should result in `x` being `[1 2 3]`.
66
* **BREAKING**: `:validation-errors :error-handler`, `:validation-errors :catch-core-errors?`
77
and `:exceptions :exception-handler` options have been removed.
8-
* These have been replaced with general `:exceptions :handler` options.
8+
* These have been replaced with general `:exceptions :handlers` options.
9+
* Fails nicely at compile-time
910
* **BREAKING**: New handler use different arity than old handler functions.
11+
* new arguments: Exception, ex-info and request.
1012
* Move `context` from `compojure.api.sweet` to `compojure.api.legacy`. Use `context*` instead.
1113
* Updated deps:
1214

1315
```clojure
1416
[metosin/ring-swagger "0.21.0-SNAPSHOT"] is available but we use "0.20.4"
1517
[compojure "1.4.0"] is available but we use "1.3.4"
1618
[prismatic/schema "0.4.4"] is available but we use "0.4.3"
17-
[metosin/ring-http-response "0.6.4"] is available but we use "0.6.3"
19+
[metosin/ring-http-response "0.6.5"] is available but we use "0.6.3"
1820
[metosin/schema-tools "0.5.2"] is available but we use "0.5.1"
1921
[metosin/ring-swagger-ui "2.1.2"] is available but we use "2.1.5-M2"
2022
[peridot "0.4.1"] is available but we use "0.4.0"

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ lein new compojure-api my-api +clojure-test
138138
There is prepackaged middleware `compojure.api.middleware/api-middleware` for common web api usage. It's a enhanced version of `compojure.handler/api` adding the following:
139139

140140
- catching slingshotted http-errors (`ring.middleware.http-response/catch-response`)
141-
- catching model validation errors (`ring.swagger.middleware/catch-validation-errors`)
142-
- catching unhandler exceptions (`compojure.api.middleware/wrap-exceptions`)
141+
- catching unhandled exceptions (`compojure.api.middleware/wrap-exceptions`)
143142
- support for different protocols via `ring.middleware.format-params/wrap-restful-params` and `ring.middleware.format-response/wrap-restful-response`
144143
- default supported protocols are: `:json-kw`, `:yaml-kw`, `:edn`, `:transit-json` and `:transit-msgpack`
145144
- enabled protocol support is also published into Swagger docs via `ring.swagger.middleware/wrap-swagger-data`.
@@ -433,6 +432,38 @@ the `:components` restucturing with letk-syntax.
433432

434433
To see this in action, try `lein run` and navigate to Components api group.
435434

435+
## Exception handling
436+
437+
All exceptions should be handled gracefully. Compojure-api ships with customizable exception handling with good
438+
defaults. Customization is done via `api` options - delegating to `compojure.api.middleware/wrap-exceptions`, which
439+
does the real work. It catches all thrown exceptions and selects a custom handler based on the thrown exception
440+
`ex-data` or Slingshot value of key `:type`. If an exception doesn't have ex-data (e.g. legacy Java Exceptions),
441+
`:compojure.api.exception/default` type is used. Exception handlers are 3-arity functions, getting the exception,
442+
ex-data and request as arguments. Below are the default type definitions and default handling:
443+
444+
| type | what | default
445+
|-----------------------------------------------|-------------------------------------|---------
446+
|`:compojure.api.exception/request-parsing` | Input data de-serialization errors. | 400 + error in body
447+
|`:compojure.api.exception/request-validation` | Request Schema coercion errors. | 400 + schema error in body
448+
|`:compojure.api.exception/response-validation` | Response Schema coercion errors. | 500 + schema error in body
449+
|`:compojure.api.exception/default` | Everything else. | 500 + print stacktrace + safe message
450+
451+
example to override the default case + add a custom exception type + handler for it:
452+
453+
```clojure
454+
(defn custom-handler [^Exception e data request]
455+
(internal-server-error {:message (.getMessage e)}))
456+
457+
(defn calm-handler [^Exception e data request]
458+
(enhance-your-calm {:message (.getMessage e), :data data}))
459+
460+
(defapi
461+
{:exceptions {:handlers {:compojure.api.exception/default custom-handler
462+
::calm calm-handler}}}
463+
(GET* "/bang" [] (throw (RuntimeException. "kosh")))
464+
(GET* "/calm" [] (throw (ex-info "fail" {:type ::calm, :oil "snake"}))))
465+
```
466+
436467
## Schemas
437468

438469
Compojure-api uses the [Schema](https://github.com/Prismatic/schema) to describe data models, backed up by

project.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject metosin/compojure-api "0.23.0-SNAPSHOT"
1+
(defproject metosin/compojure-api "0.23.0"
22
:description "Compojure Api"
33
:url "https://github.com/metosin/compojure-api"
44
:license {:name "Eclipse Public License"
@@ -11,8 +11,8 @@
1111
[compojure "1.4.0"]
1212
[prismatic/schema "0.4.4"]
1313
[org.tobereplaced/lettercase "1.0.0"]
14-
[metosin/ring-http-response "0.6.4"]
15-
[metosin/ring-swagger "0.21.0-SNAPSHOT"]
14+
[metosin/ring-http-response "0.6.5"]
15+
[metosin/ring-swagger "0.21.0"]
1616
[metosin/schema-tools "0.5.2"]
1717
[metosin/ring-middleware-format "0.6.0"]
1818
[backtick "0.3.3"]

src/compojure/api/exception.clj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
Error response only contains class of the Exception so that it won't accidentally
1717
expose secret details."
18-
[^Exception e data request]
18+
[^Exception e _ _]
1919
(.printStackTrace e)
2020
(internal-server-error {:type "unknown-exception"
2121
:class (.getName (.getClass e))}))
@@ -33,22 +33,22 @@
3333

3434
(defn response-validation-handler
3535
"Creates error response based on Schema error."
36-
[_ data request]
36+
[_ data _]
3737
(internal-server-error {:errors (stringify-error (su/error-val data))}))
3838

3939
(defn request-validation-handler
4040
"Creates error response based on Schema error."
41-
[_ data request]
41+
[_ data _]
4242
(bad-request {:errors (stringify-error (su/error-val data))}))
4343

4444
(defn schema-error-handler
4545
"Creates error response based on Schema error."
46-
[ex data request]
46+
[_ data _]
4747
; FIXME: Why error is not wrapped to ErrorContainer here?
4848
(bad-request {:errors (stringify-error (:error data))}))
4949

5050
(defn request-parsing-handler
51-
[^Exception ex data request]
51+
[^Exception ex _ _]
5252
(let [cause (.getCause ex)]
5353
(bad-request {:type (cond
5454
(instance? JsonParseException cause) "json-parse-exception"

0 commit comments

Comments
 (0)