Mercurial > lasercutter
view src/clojure/contrib/except.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; except.clj10 ;;11 ;; Provides functions that make it easy to specify the class, cause, and12 ;; message when throwing an Exception or Error. The optional message is13 ;; formatted using clojure.core/format.14 ;;15 ;; scgilardi (gmail)16 ;; Created 07 July 200818 (ns19 ^{:author "Stephen C. Gilardi",20 :doc "Provides functions that make it easy to specify the class, cause, and21 message when throwing an Exception or Error. The optional message is22 formatted using clojure.core/format."}23 clojure.contrib.except24 (:import (clojure.lang Reflector)))26 (declare throwable)28 (defn throwf29 "Throws an Exception or Error with an optional message formatted using30 clojure.core/format. All arguments are optional:32 class? cause? format? format-args*34 - class defaults to Exception, if present it must name a kind of35 Throwable36 - cause defaults to nil, if present it must be a Throwable37 - format is a format string for clojure.core/format38 - format-args are objects that correspond to format specifiers in39 format."40 [& args]41 (throw (throwable args)))43 (defn throw-if44 "Throws an Exception or Error if test is true. args are those documented45 for throwf."46 [test & args]47 (when test48 (throw (throwable args))))50 (defn throw-if-not51 "Throws an Exception or Error if test is false. args are those documented52 for throwf."53 [test & args]54 (when-not test55 (throw (throwable args))))57 (defn throw-arg58 "Throws an IllegalArgumentException. All arguments are optional:60 cause? format? format-args*62 - cause defaults to nil, if present it must be a Throwable63 - format is a format string for clojure.core/format64 - format-args are objects that correspond to format specifiers in65 format."66 [& args]67 (throw (throwable (cons IllegalArgumentException args))))69 (defn- throwable?70 "Returns true if x is a Throwable"71 [x]72 (instance? Throwable x))74 (defn- throwable75 "Constructs a Throwable with optional cause and formatted message. Its76 stack trace will begin with our caller's caller. Args are as described77 for throwf except throwable accepts them as list rather than inline."78 [args]79 (let [[arg] args80 [class & args] (if (class? arg) args (cons Exception args))81 [arg] args82 [cause & args] (if (throwable? arg) args (cons nil args))83 message (when args (apply format args))84 ctor-args (into-array Object85 (cond (and message cause) [message cause]86 message [message]87 cause [cause]))88 throwable (Reflector/invokeConstructor class ctor-args)89 our-prefix "clojure.contrib.except$throwable"90 not-us? #(not (.startsWith (.getClassName %) our-prefix))91 raw-trace (.getStackTrace throwable)92 edited-trace (into-array StackTraceElement93 (drop 3 (drop-while not-us? raw-trace)))]94 (.setStackTrace throwable edited-trace)95 throwable))