Skip to content

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

@nenadalm

Description

@nenadalm

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

?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions