### Clojure: `have` macro expansion example Source: https://github.com/taoensso/truss/blob/master/README.md Shows the source code expansion of the `have` macro, demonstrating how it checks a predicate against an argument and throws a detailed exception if the predicate returns false. ```clojure (have string? my-arg) ; -> expands to: (if (string? my-arg) my-arg ; Returns given arg on success (throw-detailed-exception!)) ``` -------------------------------- ### Clojure: Example Truss assertion failure output Source: https://github.com/taoensso/truss/blob/master/README.md Presents a typical exception output generated by a failed Truss assertion. It highlights the detailed information included in the `truss/ex-info`, such as the timestamp, namespace, failed predicate, tested argument, and source coordinates. ```clojure (have string? (/ 1 0)) ; => ;; Truss assertion failed at truss-examples[29 1]: ;; (clojure.core/string? (/ 1 0)) ;; Error evaluating arg: Divide by zero ;; {:inst #inst "2025-02-21T14:19:36.798972000-00:00", ;; :ns "truss-examples", ;; :pred clojure.core/string?, ;; :arg ;; {:form (/ 1 0), :value :truss/exception, :type :truss/exception}, ;; :coords [29 1]} ``` -------------------------------- ### Clojure 'have' Macro Example Source: https://github.com/taoensso/truss/wiki/2-Assertions Demonstrates the basic usage of the 'have' macro in Clojure to assert that a function argument satisfies a given predicate. If the assertion fails, it throws a detailed error. The 'have' macro returns the argument if the assertion succeeds. ```clojure (defn greet "Given a string username, prints a greeting message." [username] (println "hello" (have string? username))) ``` -------------------------------- ### Testing Exceptions with Truss (Clojure) Source: https://github.com/taoensso/truss/blob/master/README.md Demonstrates how to use Truss's testing utilities to assert specific exception conditions. `throws?` can check for any exception type or specific `ex-info` data, including nested causes. ```clojure (is (throws? :any "Divide by zero" (/ 1 0))) => true (is (throws? :ex-info {:user-name :stu, :user-id pos-int?} ...)) ``` -------------------------------- ### Clojure: Basic `have` macro usage Source: https://github.com/taoensso/truss/blob/master/README.md Demonstrates the fundamental usage of the `have` macro for asserting data types and non-nil values. It shows successful assertions and how failures result in detailed exceptions. The macro can take multiple arguments and defaults to `some?` if no predicate is provided. ```clojure (have string? "foo") ; => "foo" (have string? 5) ; => Throws detailed exception ;; Multiple args can be given: (have string? "foo" "bar") ; => ["foo" "bar"] (have string? "foo" 5) ; => Throws detailed exception ;; Omit predicate to default to `some?` (non-nil): (have "foo") ; => "foo" (have false) ; => false (have nil) ; => Throws detailed exception ``` -------------------------------- ### Clojure: `have` macro with options and special predicates Source: https://github.com/taoensso/truss/blob/master/README.md Illustrates advanced usage of the `have` macro, including adding custom data to exceptions using `:data`, asserting within collections using `:in`, and utilizing built-in special predicates for common checks like nil, keyword sets, and numeric comparisons. ```clojure ;; Add arb optional info to thrown ex-data using `:data`: (have string? "foo" :data {:user-id 101}) => "foo" ;; Assert inside collections using `:in`: (have string? :in #{"foo" "bar"}) ; => #{"foo" "bar"} ;; Several special predicates are supported: (have [:or nil? string?] "foo") ; => "foo" (have [:ks<= #{:a :b}] my-map) ; => Map, or throws (have [:el #{:a :b :c}] my-arg) ; => Arg, or throws (have [:n<= 10] my-coll) ; => Coll, or throws ``` -------------------------------- ### Wrap Ring Handler with Contextual Exceptions (Clojure) Source: https://github.com/taoensso/truss/blob/master/README.md This function wraps a Ring handler, ensuring that any `truss/ex-info` exceptions thrown within the handler include the Ring request in their context. It utilizes `truss/with-ctx+` to merge the request data into the `truss/*ctx*` dynamic binding. ```clojure (defn wrap-ring-ctx "Wraps given Ring handler so that the Ring req will be included in any thrown `truss/ex-info`s." [ring-handler-fn] (fn [ring-req] (truss/with-ctx+ {:ring-req ring-req} ; Merge into `truss/*ctx*` (ring-handler-fn ring-req)))) ``` -------------------------------- ### Truss 'have' Macro Variants Source: https://github.com/taoensso/truss/wiki/2-Assertions Explains the different variants of the 'have' macro in Truss. These variants share the same syntax and failure behavior but differ in their return values on success and whether they are subject to elision by the '*assert*' setting. ```clojure ; have: Returns given arg/s, subject to elision by *assert* (have ) ; have!: Returns given arg/s, not subject to elision by *assert* (have! ) ; have?: Returns true, subject to elision by *assert* (have? ) ; have!?: Returns true, not subject to elision by *assert* (have!? ) ``` -------------------------------- ### Elide Truss assertions in Leiningen project (Clojure) Source: https://github.com/taoensso/truss/wiki/2-Assertions Shows how to configure a Leiningen project to elide Truss assertions during macro expansion by setting the global variable `*assert*` to `false` in `project.clj`. This removes assertion overhead in production builds. ```clojure ;; project.clj :global-vars {*assert* false} ``` -------------------------------- ### Configure failed assertion handler in Truss (Clojure) Source: https://github.com/taoensso/truss/wiki/2-Assertions Demonstrates how to modify the behavior of failed assertions in Truss by using the `*failed-assertion-handler*` dynamic variable. This allows capturing failures for unit testing or emitting them as signals before throwing an exception. ```clojure (set! *failed-assertion-handler* (fn [failure-details] ;; Custom failure handling logic here (println "Assertion failed:") (println failure-details))) ;; Example usage (assuming a failing assertion) ;; (truss/assert-expr (truss/is (> 1 2))) ``` -------------------------------- ### Prevent Truss assertion elision with have! (Clojure) Source: https://github.com/taoensso/truss/wiki/2-Assertions Illustrates the use of the `have!` macro from Truss to ensure assertions are never elided, even when `*assert*` is disabled. This is useful for critical checks like security assertions that must always be enforced. ```clojure (require '[taoensso.truss :as truss :refer [have!]]) (defn get-restricted-resource [ring-session] (let [auth (have! string? (:auth-token ring-session))] ; Never elide! ;; Do stuff... "Return restricted resource content")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.