Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from FarmLogs/uncaught-exceptions
Browse files Browse the repository at this point in the history
Implement uncaught exception handling in dispatcher.
  • Loading branch information
briprowe authored Sep 19, 2016
2 parents 80e5b8e + 87a4347 commit 4a642b7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
(defproject com.farmlogs/dispatcher "1.0.0"
(defproject com.farmlogs/dispatcher "1.0.1"
:description "Library to start specific workers in a jar"
:min-lein-version "2.0.0"
:plugins [[s3-wagon-private "1.2.0"]]
:repositories {"farmlogs-internal" {:url "s3p://fl-maven-repo/mvn"
:username ~(System/getenv "AMAZON_KEY")
:passphrase ~(System/getenv "AMAZON_SECRET")}}
:dependencies [[org.clojure/clojure "1.6.0"]]
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/tools.logging "0.3.1"]]
:main dispatcher.core
:profiles {:uberjar {:aot :all}})
5 changes: 4 additions & 1 deletion src/dispatcher/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
(:gen-class)
(:require [clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :refer [join]]))
[clojure.string :refer [join]]
[dispatcher.exceptions.uncaught
:refer [register-uncaught-exception-handler]]))

(defn read-config
[]
Expand All @@ -20,6 +22,7 @@
(let [config-keys (keys (read-config))]
(println "Please specify one of:" (join ", " config-keys) ".")))
([entry-point & args]
(register-uncaught-exception-handler)
(-> (read-config)
(get entry-point)
(dispatch args))))
7 changes: 7 additions & 0 deletions src/dispatcher/exceptions/protocols.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns dispatcher.exceptions.protocols)

(defprotocol HandleException
"This protocol is an extension point enabling services to define
behavior specific to a particular exception type when it's caught by
the uncaught exception handler."
(handle [exception thread]))
17 changes: 17 additions & 0 deletions src/dispatcher/exceptions/uncaught.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns dispatcher.exceptions.uncaught
(:require [clojure.tools.logging :as log]
[dispatcher.exceptions.protocols :as p]))

;; Define default behavior for all exceptions. This can be overridden
;; by downstream code. It can also be extended to more specific types.
(extend-protocol p/HandleException
Throwable
(handle [exception thread]
(log/error exception "Uncaught error on thread:" (.getName thread))))

(defn register-uncaught-exception-handler
([]
(Thread/setDefaultUncaughtExceptionHandler
(reify Thread$UncaughtExceptionHandler
(uncaughtException [_ thread throwable]
(p/handle throwable thread))))))

0 comments on commit 4a642b7

Please sign in to comment.