Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assoc error in request ex-info data #25

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

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