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

Possibility to write default error handler if something goes wrong #40

Open
nenadalm opened this issue Aug 21, 2019 · 1 comment
Open

Comments

@nenadalm
Copy link
Contributor

Hi. Instead of using callbacks in handlers/middlewares I prefer to use promises.

The reason is that if I (or somebody else) forget to handle some error - I can still set some handling and return 500 instead of waiting for timeout.

Currently I am using this middleware to take care of unexpected errors in my handlers:

(ns app.routes.middleware.rejection
  (:require
   [taoensso.timbre :as timbre]))

(defn wrap-rejection
  "Middleware returns `500` if handler returned rejected promise and logs the error."
  [handler]
  (fn [req res raise]
    (let [result (handler req res raise)]
      (when (instance? js/Promise result)
        (.catch result
                (fn [e]
                  (timbre/error e "Rejection middleware caught error")
                  (res {:status 500})))))))

Would you accept possible prs that would convert callbacks into promises like

?

@yogthos
Copy link
Member

yogthos commented Aug 21, 2019

Hi, yeah I agree that promises are easier to work with and a PR would be very welcome.

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

No branches or pull requests

2 participants