Permalink
Browse files

Syntax errors encountered by (validate-config) now helpful.

Hey, file names and line numbers!
  • Loading branch information...
1 parent c378e87 commit 6d02b2db545a23edf397745e2eb2c1b5f581d0cf @aphyr aphyr committed Dec 6, 2012
Showing with 19 additions and 6 deletions.
  1. +1 −0 project.clj
  2. +6 −5 src/riemann/config.clj
  3. +12 −1 src/riemann/logging.clj
View
@@ -25,6 +25,7 @@
[cheshire "5.0.0"]
[clj-librato "0.0.2"]
[clj-time "0.4.3"]
+ [clj-wallhack "1.0"]
[com.boundary/high-scale-lib "1.0.3"]
[com.draines/postal "1.8.0"]
[incanter/incanter-charts "1.3.0"]
View
@@ -131,15 +131,16 @@
(defn validate-config
"Check that a config file has valid syntax."
[file]
- (read-strings (slurp file)))
+ (try
+ (read-strings (slurp file))
+ (catch clojure.lang.LispReader$ReaderException e
+ (throw (logging/nice-syntax-error e file)))))
(defn include
"Include another config file.
(include \"foo.clj\")"
[file]
(binding [*ns* (find-ns 'riemann.config)]
- (let [text (slurp file)]
- ; Validate syntax
- (read-strings text)
- (load-string text))))
+ (validate-config file)
+ (load-string (slurp file))))
View
@@ -12,7 +12,8 @@
(org.apache.log4j.spi RootLogger))
(:import (org.apache.log4j.rolling TimeBasedRollingPolicy
RollingFileAppender))
- (:import org.apache.commons.logging.LogFactory))
+ (:import org.apache.commons.logging.LogFactory)
+ (:require wall.hack))
(defn set-level
"Set the level for the given logger, by string name. Use:
@@ -59,3 +60,13 @@
(.addAppender (Logger/getLogger loggername)
(doto (FileAppender.)
(.setLayout riemann-layout))))
+
+(defn nice-syntax-error
+ "Rewrites clojure.lang.LispReader$ReaderException to have error messages that
+ might actually help someone."
+ ([e] (nice-syntax-error e "(no file)"))
+ ([e file]
+ ; Lord help me.
+ (let [line (wall.hack/field (class e) :line e)
+ msg (.getMessage (or (.getCause e) e))]
+ (RuntimeException. (str "Syntax error (" file ":" line ") " msg)))))

0 comments on commit 6d02b2d

Please sign in to comment.