Browse Source

Chapter 11 commit (Errors/Exceptions).

This was hard to follow because the translation was quite bad. The
gist of was use 'throw-code' for quick and basic relay of error (using
HTTP response codes) and use 'on-exception' method to generate a more
involved HTML page to communicate the error (HTTP response code). At
least, I think that was the take-away.
master
Craig Oates 2 years ago
parent
commit
6765a2c30e
  1. 84
      src/web.lisp
  2. 9
      templates/_errors/bad-request.html
  3. 7
      templates/_errors/not-found.html
  4. 26
      templates/layouts/error.html

84
src/web.lisp

@ -27,7 +27,6 @@
;;
;; Routing rules
(defroute "/" ()
"Top page"
(let ((articless (articles-make 5)))
@ -38,7 +37,7 @@
,@(roles)
:token ,(token)
:alert ,(flash-gethash :alert ningle:*session*)
;; :news ,articles
:news ,articless
:blogs (1 2 3 4 5)
:articles ,articless))))
@ -1166,3 +1165,84 @@ nil "/users/~D"(mito:object-id user))))))))
(:or (:= "public" :status)
(:= "member-only" :status)))))))
(sxql:order-by (:desc :date-posted)) (when limit (sxql:limit limit)))))
;;; -------------------------------------------------------------------------
;;; Custom Error Pages
;;; 'throw-code' seems to be the most convenient way to generate an
;;; error page. But, it does not lend itself to generating a more
;;; involved error page like the 'on-exception' defmethod's do.
;;; (throw-code 405 :allow "put get" :method "post"))
(defroute "/error-test" ()
;; Change the error code (I.E. 405) to test different errors.
(throw-code 405 :allow "put get" :method "post"))
(defmethod on-exception ((app <web>) (code method-not-allowed))
(setf (getf (lack.response:response-headers ningle:*response*):allow)
(allow code))
(format nil "Unknown method ~S" (not-allowed code)))
(defmethod on-exception ((app <web>) (code method-not-allowed))
`(,(caveman2.exception:exception-code code)
(:allow ,(allow code))
(,(format nil "Unknown method ~S" (not-allowed code)))))
;;; The original 'on-exception' 404 error is higher up this file.
(defmethod on-exception ((app <web>) (code (eql 404)))
(declare (ignore app))
(render "_errors/not-found.html")) ; You can adjust the '404 page'.
;;; Here is a commented-out copy of it for quick reference.
;; (defmethod on-exception ((app <web>) (code (eql 404)))
;; (declare (ignore app))
;; (merge-pathnames #P"_errors/404.html"
;; *template-directory*))
#| I DO NOT KNOW WHAT 'STATUS-CODE:+BAD-REQUEST+' IS REFERRING TO
======================================================================
This method is taken directly from Chapter 11. The 'status-code' part
is not recognised which means I cannot compile the program (or use
quickload). Because of this I have commented it out so I can keep it
as a reference and hopefully work out where '+bad-request+' resides so
I can import it and get this piece of code working.
(defmethod on-exception ((app <web>)
(code (eql status-code:+bad-request+)))
(declare (ignore app))
(render "errors/bad-request.html"
`(:alert ,(flash-gethash :alert ningle:*session*))))
|#
#| Added in Chapter 11 (Errors)
======================================================================
Below, I think, is examples of how to form more complicated error
messages/pages/exceptions beyond what is provided by Caveman. I cannot
say for certain because the chapter's text is a bit hard to
read (Chapter 11). Google's Japanese to English translation is the
worse of all the chapters I have read up to now.
|#
(define-condition method-not-allowed (caveman2.exception:http-exception)
((allow
:initarg :allow
:reader allow)
(method
:initarg :method
:reader not-allowed)))
;;; --------------------------------------------------------------------
;;; Code examples for using 'define-condition' above.
;;; These code snippets are taken directly from Chapter 11.
#|
(let ((caveman2.exception:*exception-class*
'method-not-allowed))
(throw-code 405 :allow "put get" :method "post"))
(error 'method-not-allowed :method "post" :allow "put get" :code 405)
`(405 (:allow "put get")(,(format nil "Unknown method ~S""post")))
|#

9
templates/_errors/bad-request.html

@ -0,0 +1,9 @@
{% extends "layouts/error.html" %}
{% block title %}{% lisp (title! "Bad Request") %}{% endblock %}
{% block main %}
<h1>400 {% lisp (title!) %}</h1>
{% if alert %}
<p class="notice">{{alert}}</p>
{% endif %}
{% endblock %}

7
templates/_errors/not-found.html

@ -0,0 +1,7 @@
{% extends "layouts/error.html" %}
{% block title %} {% lisp (title! "Not Found") %} {% endblock %}
{% block main %}
<h1>404 Not Found</h1>
{% endblock %}

26
templates/layouts/error.html

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
{% block title %}Title{% endblock %}
</title>
<link rel="stylesheet" media="all" href="/css/app.css">
</head>
<body>
<div id="container">
<header>
<img src="/images/lisplogo.svg" alt="Image alt text" width="800" height="400">
<nav class="menubar">
<a href="/">TOP</a>
</nav>
</header>
<main>
{% block main %}Main content here{% endblock %}
</main>
<footer>
{% include "shared/footer.html" %}
</footer>
</div>
</body>
</html>
Loading…
Cancel
Save