From 2f4f54379add6b3cf8a0430e50238cac39572c1c Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sun, 8 Oct 2023 15:33:03 +1100 Subject: Fix error handling user-error translates to 404 all other errors translates to 500 without extra message that may leak server info --- bom.el | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'bom.el') diff --git a/bom.el b/bom.el index e818983..06b9784 100644 --- a/bom.el +++ b/bom.el @@ -45,11 +45,14 @@ (defun bom-api (state) "Get weather forecast data of STATE from the BOM FTP." (if-let ((filename (alist-get (intern (downcase state)) bom-state-files))) - (progn - (with-current-buffer - (find-file-noselect - (format "/ftp:anonymous@ftp.bom.gov.au:/anon/gen/fwo/%s.xml" filename)) - (libxml-parse-xml-region (point-min) (point-max)))) + (let* ((buffer (find-file-noselect + (format + "/ftp:anonymous@ftp.bom.gov.au:/anon/gen/fwo/%s.xml" + filename))) + (result (with-current-buffer buffer + (libxml-parse-xml-region (point-min) (point-max))))) + (kill-buffer buffer) + result) (user-error "State %s not found" state))) (defun bom-get-areas (resp) @@ -182,11 +185,16 @@ We use the description of the first root as the state name." (ws-start (lambda (request) (with-slots (process headers) request - (ws-response-header process 200 '("Content-type" . "text/html")) - (process-send-string - process - (bom-serve (alist-get :GET headers)) - ))) + (condition-case res + (bom-serve (alist-get :GET headers)) + (user-error (ws-send-404 process + (format "%s" (cadr res)))) + (error (ws-send-500 process)) + (:success + (ws-response-header + process 200 '("Content-type" . "text/html")) + (process-send-string process res)) + ))) bom-port))) (defun bom-stop () -- cgit v1.2.3