Skip to content

Instantly share code, notes, and snippets.

@stuartsierra
Last active August 14, 2018 16:45
Show Gist options
  • Save stuartsierra/5788531 to your computer and use it in GitHub Desktop.
Save stuartsierra/5788531 to your computer and use it in GitHub Desktop.
Cannot convey data from an exception within a Datomic transaction
;; Example showing that I cannot convey data from an exception within
;; a transaction.
(ns example
(:require [datomic.api :as d])) ; Datomic Free 0.8.4007
(def uri "datomic:mem:example")
(d/create-database uri)
(def conn (d/connect uri))
;; I define a database function that throws ex-info in a failure case.
@(d/transact
conn
[{:db/id (d/tempid :db.part/db)
:db/ident :example/retract-or-throw
:db/fn (d/function
'{:lang "clojure"
:params [db e a v]
:code (if (seq (datomic.api/q '[:find ?e :in $ ?e ?a ?v :where
[?e ?a ?v]]
db e a v))
[[:db/retract e a v]]
(throw (ex-info "Not asserted" {:e e :a a :v v})))})}])
(def err
(try
@(d/transact
conn
[[:example/retract-or-throw 10 20 30]])
(catch Exception e
e)))
;; The exception I get back does not include the map on the exception
;; I threw:
(ex-data (.getCause err))
;;=> {:db/error :transact/bad-data}
;; I can only see the data attached to my original exception in the
;; stringified form:
(.getMessage (.getCause err))
;;=> ":transact/bad-data Error calling
;; :example/retract-or-throw(10 20 30)
;; clojure.lang.ExceptionInfo: Not asserted
;; {:e 10, :a 20, :v 30}"
@a613
Copy link

a613 commented Aug 14, 2018

This is not true anymore: https://docs.datomic.com/on-prem/exceptions.html#clojure-iexceptioninfo

@(d/transact
  conn
  [{:db/id (d/tempid :db.part/db)
    :db/ident :example/always-throw
    :db/fn (d/function
            '{:lang "clojure"
              :params [db]
              :code (throw (ex-info "alpha" {:x 100}))})}])
user=> (ex-data (.getCause err))
{:x 100}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment