aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-10-08 15:33:03 +1100
committerYuchen Pei <id@ypei.org>2023-10-08 15:33:03 +1100
commit2f4f54379add6b3cf8a0430e50238cac39572c1c (patch)
treebb99ecdb30ad01b37af3b750e2fd8997cec05555
parent4bfd3e011fabf486882174aaf86f462533877b22 (diff)
Fix error handling
user-error translates to 404 all other errors translates to 500 without extra message that may leak server info
-rw-r--r--bom.el28
1 files changed, 18 insertions, 10 deletions
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 ()