Skip to content

Instantly share code, notes, and snippets.

@stuarthalloway
Last active August 22, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuarthalloway/2c4d0244073de8be8889bc359bcb77e8 to your computer and use it in GitHub Desktop.
Save stuarthalloway/2c4d0244073de8be8889bc359bcb77e8 to your computer and use it in GitHub Desktop.
Intended for form-at-a-time at the REPL
(require
'[clojure.main :as main]
'[clojure.pprint :as pp])
(def strings
"Some strings that should fail somewhere in read/compile"
[":::5"
"(let [x])"
"(cond 1)"
"defmulti 5 class)"
"(def 5)"
"(/ 1 0)"
"(+ 1 :a)"
"(f :a)"
"(assert false)"])
(defn string-and-error
"Returns a map of
:string s
:error error string from read+eval of s"
[s]
(try
(-> s read-string eval)
(throw (ex-info "Expected an exception" {:string s}))
(catch Throwable t
{:string s
:error (main/ex-str t)})))
(->> strings (map string-and-error) pp/pprint)
(comment
({:string ":::5",
:error
"Evaluation error at clojure.lang.Util.runtimeException (Util.java:221). RuntimeException Invalid token: :::5\n"}
{:string "(let [x])",
:error
"Syntax error macroexpanding clojure.core/let at (39:1). Cause: Call to clojure.core/let did not conform to spec.\nIn: [0] val: () fails spec: :clojure.core.specs/bindings at: [:args :bindings :init-expr] predicate: any?, Insufficient input\n"}
{:string "(cond 1)",
:error
"Syntax error macroexpanding cond at (39:1). Cause: cond requires an even number of forms\n"}
{:string "defmulti 5 class)",
:error
"Syntax error compiling at (39:1). Cause: Can't take value of a macro: #'clojure.core/defmulti\n"}
{:string "(def 5)",
:error
"Syntax error compiling def at (39:1). Cause: First argument to def must be a Symbol\n"}
{:string "(/ 1 0)",
:error
"Evaluation error at clojure.lang.Numbers.divide (Numbers.java:163). ArithmeticException Divide by zero\n"}
{:string "(+ 1 :a)",
:error
"Evaluation error at clojure.lang.Numbers.add (Numbers.java:128). ClassCastException clojure.lang.Keyword cannot be cast to java.lang.Number\n"}
{:string "(f :a)",
:error
"Syntax error compiling at (39:1). Cause: Unable to resolve symbol: f in this context\n"}
{:string "(assert false)",
:error
"Evaluation error at user/eval9507 (NO_SOURCE_FILE:39). AssertionError Assert failed: false\n"})
)
(defn string-and-load-error
"Returns a map of
:string s
:error error string from calling load on a file containing s
Overwrites __string_and_load_error.clj"
[s]
(try
(spit "__string_and_load_error.clj" s)
(load-file "__string_and_load_error.clj")
(throw (ex-info "Expected an exception" {:string s}))
(catch Throwable t
{:string s
:error (main/ex-str t)})))
(->> strings (map string-and-load-error) pp/pprint)
(comment
({:string ":::5",
:error
"Syntax error reading source at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: Invalid token: :::5\n"}
{:string "(let [x])",
:error
"Syntax error macroexpanding clojure.core/let at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: Call to clojure.core/let did not conform to spec.\nIn: [0] val: () fails spec: :clojure.core.specs/bindings at: [:args :bindings :init-expr] predicate: any?, Insufficient input\n"}
{:string "(cond 1)",
:error
"Syntax error macroexpanding cond at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: cond requires an even number of forms\n"}
{:string "defmulti 5 class)",
:error
"Syntax error compiling at (/Users/stu/code/clojure/__string_and_load_error.clj:69:1). Cause: Can't take value of a macro: #'clojure.core/defmulti\n"}
{:string "(def 5)",
:error
"Syntax error compiling def at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: First argument to def must be a Symbol\n"}
{:string "(/ 1 0)",
:error
"Syntax error compiling at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: Divide by zero\n"}
{:string "(+ 1 :a)",
:error
"Syntax error compiling at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: clojure.lang.Keyword cannot be cast to java.lang.Number\n"}
{:string "(f :a)",
:error
"Syntax error compiling at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: Unable to resolve symbol: f in this context\n"}
{:string "(assert false)",
:error
"Syntax error compiling at (/Users/stu/code/clojure/__string_and_load_error.clj:1:1). Cause: Assert failed: false\n"})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment