Merge pull request #77 from rwiker/master
[hunchentoot.git] / conditions.lisp
blob4008d7471f79e6e0669b57152b35cb55a6aff395
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
3 ;;; Copyright (c) 2008-2009, Dr. Edmund Weitz. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package :hunchentoot)
31 (define-condition hunchentoot-condition (condition)
33 (:documentation "Superclass for all conditions related to Hunchentoot."))
35 (define-condition hunchentoot-error (hunchentoot-condition error)
37 (:documentation "Superclass for all errors related to Hunchentoot."))
39 (define-condition hunchentoot-simple-error (hunchentoot-error simple-condition)
41 (:documentation "Like HUNCHENTOOT-ERROR but with formatting capabilities."))
43 (defun hunchentoot-error (format-control &rest format-arguments)
44 "Signals an error of type HUNCHENTOOT-SIMPLE-ERROR with the provided
45 format control and arguments."
46 (error 'hunchentoot-simple-error
47 :format-control format-control
48 :format-arguments format-arguments))
50 (define-condition hunchentoot-warning (hunchentoot-condition warning)
52 (:documentation "Superclass for all warnings related to Hunchentoot."))
54 (define-condition hunchentoot-simple-warning (hunchentoot-warning simple-condition)
56 (:documentation "Like HUNCHENTOOT-WARNING but with formatting capabilities."))
58 (defun hunchentoot-warn (format-control &rest format-arguments)
59 "Signals a warning of type HUNCHENTOOT-SIMPLE-WARNING with the
60 provided format control and arguments."
61 (warn 'hunchentoot-simple-warning
62 :format-control format-control
63 :format-arguments format-arguments))
65 (define-condition parameter-error (hunchentoot-simple-error)
67 (:documentation "Signalled if a function was called with incosistent or illegal parameters."))
69 (defun parameter-error (format-control &rest format-arguments)
70 "Signals an error of type PARAMETER-ERROR with the provided
71 format control and arguments."
72 (error 'parameter-error
73 :format-control format-control
74 :format-arguments format-arguments))
76 (define-condition operation-not-implemented (hunchentoot-error)
77 ((operation :initarg :operation
78 :reader hunchentoot-operation-not-implemented-operation
79 :documentation "The name of the unimplemented operation."))
80 (:report (lambda (condition stream)
81 (format stream "The operation ~A is not yet implemented for the implementation ~A.
82 Consider sending a patch..."
83 (hunchentoot-operation-not-implemented-operation condition)
84 (lisp-implementation-type))))
85 (:documentation "This warning is signalled when an operation \(like
86 SETUID for example) is not implemented for a specific Lisp."))
88 (defun not-implemented (name)
89 "Used to signal an error if an operation named NAME is not implemented."
90 (error 'operation-not-implemented :operation name))
92 (defgeneric maybe-invoke-debugger (condition)
93 (:documentation "This generic function is called whenever a
94 condition CONDITION is signaled in Hunchentoot. You might want to
95 specialize it on specific condition classes for debugging purposes.")
96 (:method (condition)
97 "The default method invokes the debugger with CONDITION if
98 *CATCH-ERRORS-P* is NIL."
99 (unless *catch-errors-p*
100 (invoke-debugger condition))))
102 (defmacro with-debugger (&body body)
103 "Executes BODY and invokes the debugger if an error is signaled and
104 *CATCH-ERRORS-P* is NIL."
105 `(handler-bind ((error #'maybe-invoke-debugger))
106 ,@body))
108 (defmacro ignore-errors* (&body body)
109 "Like IGNORE-ERRORS, but observes *CATCH-ERRORS-P*."
110 `(ignore-errors (with-debugger ,@body)))
112 (defmacro handler-case* (expression &rest clauses)
113 "Like HANDLER-CASE, but observes *CATCH-ERRORS-P*."
114 `(handler-case (with-debugger ,expression)
115 ,@clauses))
117 (defun get-backtrace ()
118 "Returns a string with a backtrace of what the Lisp system thinks is
119 the \"current\" error."
120 (handler-case
121 (with-output-to-string (s)
122 (trivial-backtrace:print-backtrace-to-stream s))
123 (error (condition)
124 (format nil "Could not generate backtrace: ~A." condition))))