This repository has been archived by the owner on Jan 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A first stab at unified error handling.
- Loading branch information
Showing
8 changed files
with
132 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(ns bodol.error | ||
(:require [bodol.types :as t])) | ||
|
||
(defn error [scope sexp type msg] | ||
(ex-info msg {:scope scope | ||
:sexp sexp | ||
:pos (t/-pos sexp) | ||
:type type})) | ||
|
||
(defn sexpless-error [scope pos type msg] | ||
(ex-info msg {:scope scope | ||
:sexp nil | ||
:pos pos | ||
:type type})) | ||
|
||
(defn raise [scope sexp type msg] | ||
(throw (error scope sexp type msg))) | ||
|
||
(defn sexpless-raise [scope pos type msg] | ||
(throw (sexpless-error scope pos type msg))) | ||
|
||
(defn- report-pos [pos] | ||
(str | ||
(if-let [location (:location pos)] | ||
(str "At \"" location "\" line ") "At line ") | ||
(:span pos) "\n")) | ||
|
||
(defn report [error] | ||
(let [{:keys [scope sexp pos type]} (ex-data error) | ||
msg (.getMessage error)] | ||
(str | ||
(report-pos pos) | ||
"Error " type "\n" msg "\n"))) | ||
|
||
(defn error-type [error] | ||
(when (instance? clojure.lang.ExceptionInfo error) | ||
(:type (ex-data error)))) | ||
|
||
(defn error? [error] | ||
(not (nil? (error-type error)))) | ||
|
||
(defmacro do-catch [& body] | ||
`(try | ||
~@body | ||
(catch clojure.lang.ExceptionInfo e# e#))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns bodol.error-reporting-test | ||
(:require [clojure.test :as test :refer [deftest is]] | ||
[bodol.repl :as repl] | ||
[bodol.parser :as parser] | ||
[bodol.scope :as scope] | ||
[bodol.error :refer [do-catch error? error-type]])) | ||
|
||
(defmacro run [forms & body] | ||
`(let ~(apply vector | ||
(mapcat (fn [[binding code]] | ||
[binding `(do-catch (repl/eval ~code))]) | ||
(partition 2 forms))) | ||
~@body)) | ||
|
||
(defmacro deftest-throws [name type code] | ||
`(deftest ~name | ||
(run [r# ~code] | ||
(is (error? r#)) | ||
(is (= ~type (error-type r#)))))) | ||
|
||
(deftest report-file-name | ||
(let [r (do-catch | ||
(repl/eval-ast (scope/scope) | ||
(parser/parse "not-in-scope" "hello.bodol")))] | ||
(is (error? r)) | ||
(is (= "hello.bodol" (:location (:pos (ex-data r))))))) | ||
|
||
(deftest-throws report-unbound-symbols | ||
:unbound-symbol "variable-not-in-scope") | ||
|
||
(deftest-throws report-arity-mismatch | ||
:arity-mismatch "((λ i -> i) 1 2)") |