mirror of
https://github.com/penpot/penpot.git
synced 2026-05-12 19:43:48 +00:00
48 lines
1.3 KiB
Clojure
48 lines
1.3 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
|
|
|
(ns app.common.exceptions
|
|
"A helpers for work with exceptions."
|
|
(:require [clojure.spec.alpha :as s]))
|
|
|
|
(s/def ::type keyword?)
|
|
(s/def ::code keyword?)
|
|
(s/def ::mesage string?)
|
|
(s/def ::hint string?)
|
|
(s/def ::cause #?(:clj #(instance? Throwable %)
|
|
:cljs #(instance? js/Error %)))
|
|
(s/def ::error-params
|
|
(s/keys :req-un [::type]
|
|
:opt-un [::code
|
|
::hint
|
|
::mesage
|
|
::cause]))
|
|
|
|
(defn error
|
|
[& {:keys [type code message hint cause] :as params}]
|
|
(s/assert ::error-params params)
|
|
(let [message (or message hint "")
|
|
payload (dissoc params :cause :message)]
|
|
(ex-info message payload cause)))
|
|
|
|
(defmacro raise
|
|
[& args]
|
|
`(throw (error ~@args)))
|
|
|
|
(defn ignoring*
|
|
[f]
|
|
(try
|
|
(f)
|
|
(catch #?(:clj Exception :cljs :default) e
|
|
nil)))
|
|
|
|
;; http://clj-me.cgrand.net/2013/09/11/macros-closures-and-unexpected-object-retention/
|
|
;; Explains the use of ^:once metadata
|
|
|
|
(defmacro ignoring
|
|
[& exprs]
|
|
`(ignoring* (^:once fn* [] ~@exprs)))
|