summaryrefslogtreecommitdiff
path: root/api.scm
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2024-07-21 14:08:42 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2024-07-21 14:26:47 -0400
commitb38ff0788e83c0364b6d6aca2b7acc50cd61dcfc (patch)
treebf8f016df81b6a60e8a9f21f6c9ff7cb1ef440cd /api.scm
parent9c7651e85975572a7c2afc30106e25ff4be9e2ef (diff)
[dynamic] Improve error logging/reporting
Diffstat (limited to 'api.scm')
-rw-r--r--api.scm25
1 files changed, 24 insertions, 1 deletions
diff --git a/api.scm b/api.scm
index 1e71064..fdc70ee 100644
--- a/api.scm
+++ b/api.scm
@@ -30,6 +30,7 @@
(rnrs conditions)
(rnrs exceptions)
(srfi srfi-1)
+ (srfi srfi-19)
(web request)
(web response)
(web server)
@@ -59,6 +60,20 @@
(panic "This API is only available on the clearnet." #:code 403)
(handler request body))))
+(define (dump-error request body endpoint)
+ (define file-name (date->string (current-date) "jakob-api-crash-report-~4.txt"))
+ (log-append! 'error (format #f "Unhandled error! Crash report written to ~a" file-name))
+ (call-with-output-file file-name
+ (lambda (port)
+ (format port "Error caused by endpoint `~a'~%~%" endpoint)
+ (format port "Full dump of `request':")
+ (write request port)
+ (format port "~%~%")
+ (format port "Full dump of `body':")
+ (write body port)
+ (format port "~%~%")
+ (display-backtrace (make-stack #t) port))))
+
(define (handle-api-request request body endpoint)
"Route handler for the API server."
(let ((method (request-method request))
@@ -71,7 +86,15 @@
;; returning multiple values from a `guard' clause.
(apply values
(guard (ex ((reportable-condition? ex)
- (values->list (format-error-response ex))))
+ (values->list (format-error-response ex)))
+ ((equal? "application/json" (assoc-ref (request-headers request) 'accept))
+ (dump-error request body endpoint)
+ (list (build-response #:code 500)
+ (scm->json-string
+ '((success . #f) (error . "Internal error.")))))
+ (#t
+ (dump-error request body endpoint)
+ (list (build-response #:code 500) "Internal server error.")))
(fail-when-ip-blacklisted originating-ip)
(values->list
(((if (%debug-enabled) identity rate-limit-wrap)