PageRenderTime 87ms CodeModel.GetById 0ms RepoModel.GetById 1ms 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
Possible License(s): LGPL-3.0
  1. (in-package :oauth)
  2. (define-condition http-error (error)
  3. ((status-code :reader http-error-status-code
  4. :initarg :status-code)
  5. (reason-phrase :reader http-error-reason-phrase
  6. :initarg :reason-phrase)))
  7. (define-condition bad-request (http-error)
  8. () (:default-initargs :status-code 400 :reason-phrase "Bad Request"))
  9. (define-condition unauthorized (http-error)
  10. () (:default-initargs :status-code 401 :reason-phrase "Unauthorized"))
  11. (defun raise-error (type &optional reason-phrase-fmt &rest reason-phrase-args)
  12. (if reason-phrase-fmt
  13. (let ((reason-phrase (apply #'format nil reason-phrase-fmt reason-phrase-args)))
  14. (error type :reason-phrase reason-phrase))
  15. (error type)))
  16. (defun default-error-handler (condition)
  17. "Default error handler for conditions of type HTTP-ERROR."
  18. (check-type condition http-error)
  19. (let ((status-code (http-error-status-code condition))
  20. (reason-phrase (http-error-reason-phrase condition)))
  21. (setf (hunchentoot:return-code*) status-code)
  22. (setf (hunchentoot:content-type*) "text/plain")
  23. (abort-request
  24. (format nil "~D ~A" status-code reason-phrase))))
  25. (defmacro protocol-assert (&body body)
  26. `(unless (progn ,@body)
  27. (raise-error 'bad-request "Failed protocol assertion")))