Skip to content

assoc error in request ex-info data#25

Open
tiensonqin wants to merge 5 commits intoplumatic:masterfrom
tiensonqin:master
Open

assoc error in request ex-info data#25
tiensonqin wants to merge 5 commits intoplumatic:masterfrom
tiensonqin:master

Conversation

@tiensonqin
Copy link

I need parse the error to show readable errors to end user,
for example:

;; validation doc
{:password "Use at least 6 characters. Don’t use a password from another site, or something too obvious like your birthdate."
 :email "Email is not legal."}

;; wrap-validation middleware
(defn wrap-validation [f]
  (fn [request]
    (try (f request)
         (catch clojure.lang.ExceptionInfo e
           (let [d (ex-data e)]
             (if (= :coercion-error (:type d))
               {:status 400
                :body {:error (select-keys (:validation doc) (keys (:error d)))}}
               (throw e)))))))

@w01fe
Copy link
Member

w01fe commented Feb 26, 2015

Thanks for the PR. I understand the use case, but I think we explicitly avoided putting :error in there since it was causing other problems in default use cases (I believe errors aren't currently serializable).

Rather than this, what do you think about allowing the user to pass a custom error handler that takes

{:schema schema
 :data data
 :context context
 :error error}

into the middleware, where the error handler is defaulted to the current behavior (fn default-error-handler [m] (throw (ex-info (-> m (assoc :type :coercion-error) (dissoc :error))))?

@tiensonqin
Copy link
Author

Yeah, I agree with u, custom error handler is going to be ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants