summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2023-10-01 12:51:47 +0200
committerEkaitz Zarraga <ekaitz@elenq.tech>2023-10-02 21:48:41 +0200
commit6f702b3635034d44d10768f6f6b535f1267b553b (patch)
tree0d9208a6c6cc6c10186df7c4050b0ca8e8ef39de
parentf44f833de3c74c8111fd87a0997441339db80a82 (diff)
requests: don't throw exceptions on errors
API returns useful information in 400 responses so throwing is not the best way to solve this.
-rw-r--r--neocities/requests.scm31
1 files changed, 11 insertions, 20 deletions
diff --git a/neocities/requests.scm b/neocities/requests.scm
index 265ebcd..a99e296 100644
--- a/neocities/requests.scm
+++ b/neocities/requests.scm
@@ -124,17 +124,16 @@
(auth #f)
(content-type #f))
- (define (response-error res)
- `(("response-code" . ,(response-code res))
- ("response-phrase" .
- ,(response-reason-phrase res))
- ("response" .
- ,(if (bytevector? body)
- (utf8->string body)
- body))))
-
- (define (response-ok? response)
- (> 100 (- (response-code response) 200) -1))
+
+ (define (decode-body response body)
+ (match (response-content-type response)
+ (('text/plain ('charset . "utf-8"))
+ (utf8->string body))
+ (('text/html ('charset . "utf-8"))
+ (utf8->string body))
+ (('application/json . rest)
+ (json-string->scm (utf8->string body)))
+ (else body))) ;; Don't know how to decode, leave it
(let-values (((response body)
(http-request
@@ -148,12 +147,4 @@
(and content-type `(Content-Type . ,content-type))
(and auth `(Authorization . ,auth))))
#:decode-body? #f)))
- (if (response-ok? response)
- (match (response-content-type response)
- (('text/plain ('charset . "utf-8"))
- (utf8->string body))
- (('application/json . rest)
- (json-string->scm (utf8->string body)))
- (else
- (throw 'neocities "Don't know how to decode "(response-content-type response))))
- (throw 'neocities (response-error response)))))
+ (values response (decode-body response body))))