Cats Api Documentation

Version: 2.2.0

cats.monad.exception

The Exception monad.

Also known as Try monad, popularized by Scala.

It represents a computation that may either result in an exception or return a successfully computed value. Is very similar to Either monad, but is semantically different.

It consists in two types: Success and Failure. The Success type is a simple wrapper like Right of Either monad. But the Failure type is slightly different from Left, because it is forced to wrap an instance of Throwable (or Error in cljs).

The most common use case of this monad is for wrap third party libraries that uses standard Exception based error handling. In normal circumstances you should use Either instead.

The types defined for Exception monad (Success and Failure) also implementes the clojure IDeref interface which facilitates libraries developing using monadic composition without forcing a user of that library to use or understand monads.

That is because when you will dereference the failure instance, it will reraise the containing exception.

exception?

(exception? v)

Return true in case of v is instance of Exception monad.

extract

(extract mv)(extract mv default)

Return inner value from exception monad.

This is a specialized version of cats.core/extract for Exception monad types that allows set up the default value.

If a provided mv is an instance of Failure type it will re raise the inner exception. If you need extract value without raising it, use cats.core/extract function for it.

failure

(failure e)(failure e message)

A failure type constructor.

If a provided parameter is an exception, it wraps it in a Failure instance and return it. But if a provided parameter is arbitrary data, it tries create an exception from it using clojure ex-info function.

Take care that ex-info function in clojurescript differs a little bit from clojure.

failure?

(failure? v)

Return true if v is an instance of the Failure type.

map->Failure

(map->Failure m__7585__auto__)

Factory function for class cats.monad.exception.Failure, taking a map of keywords to field values.

map->Success

(map->Success m__7585__auto__)

Factory function for class cats.monad.exception.Success, taking a map of keywords to field values.

success

(success v)

A Success type constructor.

It wraps any arbitrary value into success type.

success?

(success? v)

Return true if v is an instance of the Success type.

throw-exception

(throw-exception message)

throwable?

(throwable? e)

Return true if v is an instance of the Throwable or js/Error type.

try-on

macro

(try-on expr)

Wraps a computation and return success of failure.

try-or-else

macro

(try-or-else expr defaultvalue)

try-or-recover

macro

(try-or-recover expr func)

wrap

(wrap func)

Wrap a function in a try monad.

Is a high order function that accept a function as parameter and returns an other that returns success or failure depending of result of the first function.