(ns ird.p2p.db.errors
"Error values delivered on ird.p2p.db channels. All are ex-info with a
:type key:
:ird.p2p.db/config-error a precondition failed before anything started (:detail)
:ird.p2p.db/node-error Helia, libp2p or OrbitDB failed to start or answered an error (:detail)
:ird.p2p.db/dial-error dialing a peer failed (:detail)
:ird.p2p.db/append-rejected OrbitDB refused the entry: the identity may not write to this log (:detail)
:ird.p2p.db/payload-too-large a serialized payload exceeds the per-entry limit (:bytes :limit)
:ird.p2p.db/closed the database handle has been closed
Remote tampering is never an error: records that do not decrypt are
dropped silently, by design (same ethos as ird-p2p-cljs, \"decrypt or
drop\"); OrbitDB itself refuses entries whose signature or writer does not
check out before they ever reach this library."
(:require [ird.p2p.async :as a]))
(defn config-error
"Invalid configuration or arguments, detected before anything started."
[message detail]
(ex-info message {:type :ird.p2p.db/config-error :detail detail}))
(defn node-error
"Helia, libp2p or OrbitDB failed to start or answered an error."
[message detail]
(ex-info message {:type :ird.p2p.db/node-error :detail detail}))
(defn dial-error
"Dialing a peer's multiaddrs failed."
[message detail]
(ex-info message {:type :ird.p2p.db/dial-error :detail detail}))
(defn append-rejected
"OrbitDB's access controller refused the entry: the local identity is not
in the database's write list (a database opened by address with a secret
other than the one it was created with)."
[message detail]
(ex-info message {:type :ird.p2p.db/append-rejected :detail detail}))
(defn payload-too-large
"One serialized payload exceeds the per-entry limit (OrbitDB entries are
not a blob store)."
[bytes limit]
(ex-info (str "Payload of " bytes " bytes exceeds the per-entry limit of " limit)
{:type :ird.p2p.db/payload-too-large :bytes bytes :limit limit}))
(defn closed
"The database handle has been closed."
[]
(ex-info "Closed" {:type :ird.p2p.db/closed}))
(def types
"Every :type an ird.p2p.db error can carry."
#{:ird.p2p.db/config-error :ird.p2p.db/node-error :ird.p2p.db/dial-error
:ird.p2p.db/append-rejected :ird.p2p.db/payload-too-large :ird.p2p.db/closed})
(def error?
"True for any error value (any JavaScript Error, including ExceptionInfo).
Same as ird.p2p.async/error?, re-exported for convenience."
a/error?)
(defn db-error?
"True when E is an ex-info produced by this library."
[e]
(contains? types (:type (ex-data e))))
(defn config-error? [e] (= :ird.p2p.db/config-error (:type (ex-data e))))
(defn node-error? [e] (= :ird.p2p.db/node-error (:type (ex-data e))))
(defn dial-error? [e] (= :ird.p2p.db/dial-error (:type (ex-data e))))
(defn append-rejected? [e] (= :ird.p2p.db/append-rejected (:type (ex-data e))))
(defn payload-too-large? [e] (= :ird.p2p.db/payload-too-large (:type (ex-data e))))
(defn closed? [e] (= :ird.p2p.db/closed (:type (ex-data e))))