Commit2fe76508Recorded20 Mar 2026Repositorysigil-forgejo

Improve error handling: surface HTTP status and response body on failures

Changed
 src/forgejo/client.sgl | 51 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 19 deletions(-)
Diff
src/forgejo/client.sglmodified
@@ -32,38 +32,51 @@
32
(apply string-append base-url "/api/v1"
33
(map (lambda (p) (string-append "/" p)) parts)))
34
+35
;; JSON headers for requests with a body.
+36
(define (forgejo-json-headers token)
+37
(dict-merge (forgejo-auth-headers token)
+38
#{ content-type: "application/json" }))
+39
+40
;; Check an HTTP response and return parsed JSON, or raise an error
+41
;; with the status code and response body.
+42
(define (check-response response)
+43
(if (not (http-response? response))
+44
(error "Forgejo API request failed: no response"))
+45
(let ((status (http-response-status response))
+46
(body (http-response-body response)))
+47
(if (>= status 400)
+48
(error (string-append "Forgejo API error " (number->string status)
+49
": " (or body "")))
+50
(if (and body (not (string=? body "")))
+51
(json-decode body)
+52
#t))))
+53
54
;;; Authenticated JSON GET request.
55
(define (forgejo-get/json token url)
37
(http-get/json url headers: (forgejo-auth-headers token)))
+56
(check-response
+57
(http-get url headers: (forgejo-auth-headers token))))
58
59
;;; Authenticated JSON POST request.
60
(define (forgejo-post/json token url body)
41
(http-post/json url body headers: (forgejo-auth-headers token)))
+61
(check-response
+62
(http-post url (json-encode body)
+63
headers: (forgejo-json-headers token))))
64
65
;;; Authenticated JSON PUT request.
66
(define (forgejo-put/json token url body)
45
(let ((response (http-put url
46
(json-encode body)
47
headers: (dict-merge
48
(forgejo-auth-headers token)
49
#{ content-type: "application/json" }))))
50
(if (http-response? response)
51
(http-response-json response)
52
response)))
+67
(check-response
+68
(http-put url (json-encode body)
+69
headers: (forgejo-json-headers token))))
70
71
;;; Authenticated JSON PATCH request.
72
(define (forgejo-patch/json token url body)
56
(let ((response (http-patch url
57
(json-encode body)
58
headers: (dict-merge
59
(forgejo-auth-headers token)
60
#{ content-type: "application/json" }))))
61
(if (http-response? response)
62
(http-response-json response)
63
response)))
+73
(check-response
+74
(http-patch url (json-encode body)
+75
headers: (forgejo-json-headers token))))
76
77
;;; Authenticated DELETE request.
78
(define (forgejo-delete token url)
67
(http-delete url headers: (forgejo-auth-headers token)))
+79
(check-response
+80
(http-delete url headers: (forgejo-auth-headers token))))
81
82
))