From e76a59a400f7df03636a6af707b159b1bd7eaaa4 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Tue, 8 Feb 2022 15:41:54 -0600 Subject: better custom error handling --- lazybones-hunchentoot.lisp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'lazybones-hunchentoot.lisp') diff --git a/lazybones-hunchentoot.lisp b/lazybones-hunchentoot.lisp index 9c2d108..e87fbd7 100644 --- a/lazybones-hunchentoot.lisp +++ b/lazybones-hunchentoot.lisp @@ -30,12 +30,20 @@ :address "127.0.0.1")) (defmethod h:acceptor-dispatch-request ((%server lazybones-acceptor) request) - (loop for app in (acceptor-apps %server) - for (endpoint . args) = (lzb::find-endpoint app request) - when endpoint - return (lzb::run-endpoint endpoint args request h:*reply* app) - ;; if no endpoint was found, call next method. - finally (call-next-method))) + (handler-case + (loop for app in (acceptor-apps %server) + for (endpoint . args) = (lzb::find-endpoint app request) + when endpoint + return (lzb::run-endpoint endpoint args request h:*reply* app) + ;; if no endpoint was found, call next method. + finally (let ((lzb:*request* request) + (lzb:*response* h:*reply*)) + (return (lzb:http-err 404)))) + (error (e) + (declare (ignorable e)) + (let ((lzb:*request* request) + (lzb:*response* h:*reply*)) + (lzb:http-err 500))))) ;;; SERVER FUNCTIONS @@ -43,7 +51,10 @@ "Creates an opaque server on port PORT, and returns it. Servers are backend specific, but each may be passed in to INSTALL-APP, UNINSTALL-APP, START-SERVER, and STOP-SERVER." - (make-instance 'lazybones-acceptor :port port :address address)) + (let ((server (make-instance 'lazybones-acceptor :port port :address address))) + (set-canned-response server 404 "Not Found" "text/plain") + (set-canned-response server 500 "Server Error" "text/plain") + server)) (defun start-server (server) (h:start server)) -- cgit v1.2.3