Error handling for processing pipelines of information in Clojure.
The design
For API documentation, see: https://nomicflux.github.io/danger-mouse/
Capture errors for reporting while continuing to process data:
(danger-mouse.catch-errors/catch-errors-> [1 2 3]
(map inc)
(map #(if (even? %) % (throw (Exception. (str %)))))
(map #(* % 10)))
> {:result [20 40]
:errors [{:error-msg "3"
:input 2
:error ....}]}
Use try-catch
, then resolve
to apply different functions over
errors and successes, respectively:
(->> (danger-mouse.macros/try-catch (throw (Exception. "Oops")))
(danger-mouse.utils/resolve ex-message str))
> "Oops"
(->> (danger-mouse.macros/try-catch 1)
(danger-mouse.utils/resolve ex-message str))
> "1"
Update transducers to capture errors, then handle the errors while passing on successful results:
(let [collector (danger-mouse.transducers/collect
(map inc)
(map #(if (even? %)
(danger-mouse.schema/as-error %)
%)))
(take 2)
(map (partial * 10)))]
(->> (range 10)
collector
(danger-mouse.utils/handle-errors println)))
;; [2]
> [10 30]
MIT License
Copyright © 2021-2024 Michael Anderson
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Can you improve this documentation? These fine people already did:
Michael A. & Michael AEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close