org.sbrubbles.conditio

A simple condition library.

Example usage:

(require '[org.sbrubbles.conditio :as c])

;; ...

(c/handle [:condition #(c/restart :restart (:n %))]
  (c/with [:restart inc]
    (assert (= (c/signal :condition :n 1)
               2))))

*handlers*

dynamic

A map with the available handlers, stored as handler chains. Use handle to install new handlers.

A handler is a function which takes a condition and returns the value signal should return. A handler chain is a list of handlers, to be run one at a time until the first non-(skip) value is returned.

*restarts*

dynamic

A map with the available restarts. Use with to install new restarts, and restart to run them.

A restart is a function which recovers from conditions, expected to be called from a handler.

abort

(abort)(abort c)

Throws an exception, taking an optional argument. The 1-arity version also works as a handler.

condition

(condition c)(condition id & {:as args})

Creates a new condition, or returns it unchanged if it’s the sole argument. id cannot be nil.

condition?

(condition? v)

Checks if the given value is a condition: a map with :org.sbrubbles.conditio/id mapped to a non-nil value.

handle

macro

(handle bindings & body)

Takes a map of keyword/handler pairs. This macro:

  1. installs the given handlers as a thread-local binding;
  2. executes the given body;
  3. pops the given handlers after body was evaluated; and
  4. returns the value of body.

restart

(restart option & args)

Searches for a restart mapped to option, and then runs it with args.

Signals :org.sbrubbles.conditio/restart-not-found if no restart mapped to option could be found.

signal

(signal id & {:as args})

Signals a condition, searching for a handler and returning whatever it returns. id and args will be used to create the condition, as in condition.

Signals :org.sbrubbles.conditio/handler-not-found if a handler couldn’t be found.

skip

(skip)(skip _)

Returns a value which, when returned by a handler, means that it opted not to handle the condition, and the handler chain should try the next one in line.

The 1-arity version works as a handler, and returns the same value.

with

macro

(with bindings & body)

Takes a map of keyword/restart pairs, and then:

  1. installs the given restarts as a thread-local binding;
  2. executes the given body;
  3. pops the given restarts after body was evaluated; and
  4. returns the value of body.

with-fn

(with-fn restart-map f)

Returns a function, which will install the given restarts and then run f. This may be used to define a helper function which runs on a different thread, but needs the given restarts in place.