Integrate yada with Bugsnag notifier
This commit is contained in:
parent
800f466fa9
commit
767e33145f
@ -1,17 +1,34 @@
|
||||
(ns asciinema.main
|
||||
(:gen-class)
|
||||
(:require [com.stuartsierra.component :as component]
|
||||
(:require [asciinema.yada :as y]
|
||||
[clj-bugsnag.core :as bugsnag]
|
||||
[com.stuartsierra.component :as component]
|
||||
[duct.util.runtime :refer [add-shutdown-hook]]
|
||||
[duct.util.system :refer [load-system]]
|
||||
[environ.core :refer [env]]
|
||||
[clojure.java.io :as io]))
|
||||
|
||||
(defn- request-context [req]
|
||||
(str (-> req (get :request-method :unknown) name .toUpperCase)
|
||||
" "
|
||||
(:uri req)))
|
||||
|
||||
(defn- create-exception-notifier []
|
||||
(when-let [key (:bugsnag-key env)]
|
||||
(let [environment (:env-name env "production")
|
||||
version (:git-sha env)]
|
||||
(fn [ex req]
|
||||
(bugsnag/notify ex {:api-key key
|
||||
:environment environment
|
||||
:project-ns "asciinema"
|
||||
:version version
|
||||
:context (request-context req)
|
||||
:meta {:request (dissoc req :body)}})))))
|
||||
|
||||
(defn -main [& args]
|
||||
(binding [y/*exception-notifier* (create-exception-notifier)]
|
||||
(let [bindings {'http-port (Integer/parseInt (:port env "3000"))
|
||||
'db-uri (:database-url env)
|
||||
'env-name (:env-name env "production")
|
||||
'git-sha (:git-sha env)
|
||||
'bugsnag-key (:bugsnag-key env)
|
||||
's3-bucket (:s3-bucket env)
|
||||
's3-access-key (:s3-access-key env)
|
||||
's3-secret-key (:s3-secret-key env)
|
||||
@ -20,5 +37,5 @@
|
||||
system (->> (load-system [(io/resource "asciinema/system.edn")] bindings)
|
||||
(component/start))]
|
||||
(add-shutdown-hook ::stop-system #(component/stop system))
|
||||
(println "Started HTTP server on port" (-> system :http :port)))
|
||||
(println "Started HTTP server on port" (-> system :http :port))))
|
||||
@(promise))
|
||||
|
@ -4,6 +4,8 @@
|
||||
[yada.status :as status]
|
||||
[yada.yada :as yada]))
|
||||
|
||||
(def ^:dynamic *exception-notifier* nil)
|
||||
|
||||
(def not-found-model
|
||||
{:produces
|
||||
#{"text/html" "text/plain"}
|
||||
@ -22,15 +24,22 @@
|
||||
"text/html" (str "<html><body><h1>" status-name "</h1></body></html>")
|
||||
status-name)))
|
||||
|
||||
(defn logger [ctx]
|
||||
(defn create-logger []
|
||||
(let [notifier *exception-notifier*]
|
||||
(fn [ctx]
|
||||
(when-let [error (:error ctx)]
|
||||
(when (not= (-> ctx :response :status) 404)
|
||||
(log/error error))))
|
||||
(let [status (-> ctx :response :status)]
|
||||
(when (not= status 404)
|
||||
(log/error error))
|
||||
(when (and (= status 500) notifier)
|
||||
(let [ex (or (-> error ex-data :error) error)]
|
||||
(notifier ex (:request ctx))))))
|
||||
ctx)))
|
||||
|
||||
(defn resource [model]
|
||||
(let [error-statuses (set (concat (range 400 404) (range 405 600) ))]
|
||||
(-> model
|
||||
(assoc :logger logger)
|
||||
(assoc :logger (create-logger))
|
||||
(update-in [:responses 404] #(or % not-found-model))
|
||||
(update-in [:responses error-statuses] #(or % {:produces #{"text/html" "text/plain"}
|
||||
:response error-response}))
|
||||
|
Loading…
Reference in New Issue
Block a user