Back
ex-cause (clj)
(source)function
(ex-cause ex)
Returns the cause of ex if ex is a Throwable.
Otherwise returns nil.
Examples
seancorfield/next-jdbc
(ns next.jdbc-test
"Basic tests for the primary API of `next.jdbc`."
(:require [clojure.core.reducers :as r]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc :as jdbc]
[next.jdbc.connection :as c]
[next.jdbc.test-fixtures
:refer [with-test-db db ds column
default-options stored-proc?
derby? hsqldb? jtds? mssql? mysql? postgres? sqlite?]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.types :as types])
(:import (com.zaxxer.hikari HikariDataSource)
(com.mchange.v2.c3p0 ComboPooledDataSource PooledDataSource)
(java.sql ResultSet ResultSetMetaData)))
(deftest multi-rs
(when (mssql?)
(testing "script with multiple result sets"
(let [multi-rs
(jdbc/execute! (ds)
[(str "begin"
" select * from fruit;"
" select * from fruit where id < 4;"
" end")]
{:multi-rs true})]
(is (= 2 (count multi-rs)))
(is (= 4 (count (first multi-rs))))
(is (= 3 (count (second multi-rs)))))))
(when (mysql?)
(testing "script with multiple result sets"
(let [multi-rs
(jdbc/execute! (ds)
[(str "select * from fruit;"
" select * from fruit where id < 4")]
{:multi-rs true})]
(is (= 2 (count multi-rs)))
(is (= 4 (count (first multi-rs))))
(is (= 3 (count (second multi-rs)))))))
(when (stored-proc?)
(testing "stored proc; multiple result sets"
(try
(let [multi-rs
(jdbc/execute! (ds)
[(if (mssql?) "EXEC FRUITP" "CALL FRUITP()")]
{:multi-rs true})
zero-updates [{:next.jdbc/update-count 0}]]
(cond (postgres?) ; does not support multiple result sets yet
(do
(is (= 1 (count multi-rs)))
(is (= zero-updates (first multi-rs))))
(hsqldb?)
(do
(is (= 3 (count multi-rs)))
(is (= zero-updates (first multi-rs))))
(mysql?)
(do
(is (= 3 (count multi-rs)))
(is (= zero-updates (last multi-rs))))
:else
(is (= 2 (count multi-rs)))))
(catch Throwable t
(println 'call-proc (:dbtype (db)) (ex-message t) (some-> t (ex-cause) (ex-message))))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/rand [(t/? t/Num) :-> t/Num]
cc/rand-int [t/Int :-> t/Int]
cc/ex-info (t/IFn [(t/Nilable t/Str) (t/Map t/Any t/Any) :-> t/ExInfo]
[(t/Nilable t/Str) (t/Map t/Any t/Any) (t/? #?(:clj (t/Nilable Throwable) :cljs t/Any)) :-> t/ExInfo])
cc/ex-data (t/IFn [t/ExInfo :-> (t/Map t/Any t/Any)]
[t/Any :-> (t/Nilable (t/Map t/Any t/Any))])
cc/ex-message [t/Any :-> (t/Nilable t/Str)]
cc/ex-cause [t/Any :-> #?(:clj (t/Nilable Throwable) :cljs t/Any)]
fluree/db
(ns fluree.db.json-ld.api-test
(:require #?(:clj [clojure.test :refer [deftest is testing]]
:cljs [cljs.test :refer-macros [deftest is testing async]])
#?@(:cljs [[clojure.core.async :refer [go <!]]
[clojure.core.async.interop :refer [<p!]]])
[fluree.db.did :as did]
[fluree.db.dbproto :as dbproto]
[fluree.db.json-ld.api :as fluree]
[fluree.db.query.range :as query-range]
[fluree.db.test-utils :as test-utils]
[fluree.db.util.async :refer [<?? <?]]
[fluree.db.util.context :as ctx-util]
[fluree.db.util.core :as util]
#?(:clj [test-with-files.tools :refer [with-tmp-dir]
:as twf]
:cljs [test-with-files.tools :as-alias twf])))
(is (= flake-total
(:fuel response))
"Reports fuel for all the generated flakes")))
(testing "without the `:meta` option"
(let [response @(fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people})]
(is (nil? (:fuel response))
"Returns no fuel")))
(testing "short-circuits if request fuel exhausted"
(let [response @(fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people}
{:maxFuel 1})]
(is (re-find #"Fuel limit exceeded"
(-> response ex-cause ex-message))))))
(testing "queries"
(let [db @(fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people})
flake-total (-> db :stats :flakes)
query {:context test-utils/default-context
:select '[?s ?p ?o]
:where '{:id ?s, ?p ?o}}]
(testing "queries not returning metadata"
(let [sut @(fluree/query db query)]
(is (nil? (:fuel sut))
"Reports no fuel")))
(testing "queries returning metadata"
(let [query* (assoc-in query [:opts :meta] true)
sut @(fluree/query db query*)]
(is (= (dec flake-total) (:fuel sut))
"Reports that all flakes were traversed"))))
(testing "short-circuits if request fuel exhausted"
(let [query {:context test-utils/default-context
:select '[?s ?p ?o]
:where '{:id ?s
?p ?o}
:opts {:max-fuel 1}}
db @(fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people})
results @(fluree/query db query)]
(is (util/exception? results))
(is (re-find #"Fuel limit exceeded"
(-> results ex-cause ex-message))))))))
:cljs
(async done
(go
(testing "fuel tracking"
(let [conn (<! (test-utils/create-conn))
ledger (<p! (fluree/create conn "test/fuel-tracking"))
db0 (fluree/db ledger)]
(testing "transactions"
(testing "with the `:meta` option"
(let [response (<p! (fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people} {:meta true}))
db (:result response)
flake-total (- (-> db :stats :flakes)
(-> db0 :stats :flakes))]
(is (= flake-total (:fuel response))
"Reports fuel for all the generated flakes")))
(testing "without the `:meta` option"
(let [response (<p! (fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people}))]
(is (nil? (:fuel response))
"Returns no fuel")))
(testing "short-circuits if request fuel exhausted"
(let [response (try
(<p! (fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people}
{:maxFuel 1}))
(catch :default e (ex-cause e)))]
(is (re-find #"Fuel limit exceeded"
(-> response ex-cause ex-message))))))
(testing "queries"
(let [db (<p! (fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people}))
flake-total (-> db :stats :flakes)
query {:context [test-utils/default-context
{:ex "http://example.org/ns/"}]
:select '[?s ?p ?o]
:where '{:id ?s
?p ?o}}]
(testing "queries not returning metadata"
(let [sut (<p! (fluree/query db query))]
(is (nil? (:fuel sut))
"Reports no fuel")))
(testing "queries returning metadata"
(let [query* (assoc-in query [:opts :meta] true)
sut (<p! (fluree/query db query*))]
(is (= (dec flake-total) (:fuel sut))
"Reports that all flakes were traversed"))))
(testing "short-circuits if request fuel exhausted"
(let [query {:context [test-utils/default-context
{:ex "http://example.org/ns/"}]
:select '[?s ?p ?o]
:where '{:id ?s
?p ?o}
:opts {:max-fuel 1}}
db (<p! (fluree/stage db0 {"@context" ["https://ns.flur.ee"
test-utils/default-context
{:ex "http://example.org/ns/"}]
"insert" test-utils/people}))
results (try
(<p! (fluree/query db query))
(catch :default e (ex-cause e)))]
(is (util/exception? results))
(is (re-find #"Fuel limit exceeded"
(-> results ex-cause ex-message))))))))
(done)))))