From 6f702b3635034d44d10768f6f6b535f1267b553b Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Sun, 1 Oct 2023 12:51:47 +0200 Subject: requests: don't throw exceptions on errors API returns useful information in 400 responses so throwing is not the best way to solve this. --- neocities/requests.scm | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'neocities/requests.scm') 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)))) -- cgit v1.2.3