9eeb481
Branches > refactor/use-spec-1 > 9eeb481 > files
Update dev.grahp.error
Commit: 9eeb481e2846d16643897401754d3f951111f214
Parent: 0455b27
1 changed file:
src/dev/grahp/error.clj view file
@@ -1,10 +1,10 @@ (ns dev.grahp.error- "Error handling stuff idk sometimes it's annoying in clojure lol"+ "Some error handling macros." (:require [clojure.spec.alpha :as s] [clojure.string :as str])) (defmacro raise- "Throws an error with msg and an optional map and cause"+ "Throws an error with msg and an optional map and cause." {:arglists '([msg] [msg m] [msg m cause]@@ -20,37 +20,19 @@ `(throw (~msg ~m ~cause)) `(throw (ex-info ~msg ~m ~cause))))) -#_(defmacro guard- "If form throws an exception, evaluates handler with the exception as an argument."- ([form] `(guard ~form (constantly nil)))- ([form handler]- `(try ~form- (catch Exception e# (~handler e#)))))- (defmacro assert!- "Yields x if it is truthy, else throws an ex-info with optional message and map.- Only run when *assert* is truthy, otherwise yields x"- ([x] `(assert! ~x nil))- ([x msg] `(assert! ~x ~msg {}))- ([x msg m]+ "Throws an exception with an optional map and message when 'x' is falsey and *assert* is truthy,+ otherwise, returns 'x'"+ ([x] `(assert! ~x {}))+ ([x m & msg] (if *assert* `(or ~x- (raise (str "Assert Failed!" (when ~msg (str " " ~msg)) "\n"- '~x)+ (raise (str "Assert Failed!" ~@(interpose \space msg) \newline (pr-str '~x)) ~m)) x))) -#_(defmacro assert2!- "Yields x if it is truthy, else throws an ex-info with optional message and map.- Only run when *assert* is truthy, otherwise yields x"- ([x] `(assert2! ~x {}))- ([x m & msg]- (if *assert*- `(or ~x- (raise (str "Assert Failed! " ~@(interpose \space msg) \newline (pr-str '~x))- ~m))- x)))-+;; TODO these have the arguments in a different order than assert!+;; And keywords are assumed to be specs even if you want to ensure a key is present in a map... (defmacro ensure! "Yields x if it conforms to spec, else throws and ex-info with optional message and map" ([spec x] `(ensure! ~spec ~x nil))