PageRenderTime 12ms CodeModel.GetById 6ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/src/core/error-handling.lisp

http://github.com/skypher/cl-oauth
Lisp | 36 lines | 27 code | 9 blank | 0 comment | 1 complexity | 5b28dccbd9d2730217dd4cfa4238307a MD5 | raw file
 1
 2(in-package :oauth)
 3
 4
 5(define-condition http-error (error)
 6  ((status-code :reader http-error-status-code
 7                :initarg :status-code)
 8   (reason-phrase :reader http-error-reason-phrase
 9                  :initarg :reason-phrase)))
10
11(define-condition bad-request (http-error)
12  () (:default-initargs :status-code 400 :reason-phrase "Bad Request"))
13
14(define-condition unauthorized (http-error)
15  () (:default-initargs :status-code 401 :reason-phrase "Unauthorized"))
16
17(defun raise-error (type &optional reason-phrase-fmt &rest reason-phrase-args)
18  (if reason-phrase-fmt
19    (let ((reason-phrase (apply #'format nil reason-phrase-fmt reason-phrase-args)))
20      (error type :reason-phrase reason-phrase))
21    (error type)))
22
23(defun default-error-handler (condition)
24  "Default error handler for conditions of type HTTP-ERROR."
25  (check-type condition http-error)
26  (let ((status-code (http-error-status-code condition))
27        (reason-phrase (http-error-reason-phrase condition)))
28    (setf (hunchentoot:return-code*) status-code)
29    (setf (hunchentoot:content-type*) "text/plain")
30    (abort-request
31      (format nil "~D ~A" status-code reason-phrase))))
32
33(defmacro protocol-assert (&body body)
34  `(unless (progn ,@body)
35     (raise-error 'bad-request "Failed protocol assertion")))
36