Skip to content

Commit 0ff882c

Browse files
committed
gptel: Improve error extraction from JSON responses
Try a little harder to find error fields in JSON responses. Eventually the error extractor can be a generic function with API-specific methods, but it's not worth the complexity yet. * gptel-curl.el (gptel-curl--stream-cleanup): Try looking inside arrays for the error struct. (gptel-curl--parse-response): Ditto. * gptel.el (gptel--url-parse-response): Ditto.
1 parent 7e65ea1 commit 0ff882c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

gptel-curl.el

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ PROCESS and _STATUS are process parameters."
241241
(response (progn (goto-char header-size)
242242
(condition-case nil (gptel--json-read)
243243
(error 'json-read-error))))
244-
(error-data (plist-get response :error)))
244+
(error-data
245+
(cond ((plistp response) (plist-get response :error))
246+
((arrayp response)
247+
(cl-some (lambda (el) (plist-get el :error)) response)))))
245248
(cond
246249
(error-data
247250
(plist-put info :error error-data))
@@ -460,8 +463,11 @@ PROC-INFO is a plist with contextual information."
460463
((not (string-blank-p resp))))
461464
(string-trim resp))
462465
http-status http-msg))
463-
((plist-get response :error)
464-
(list nil http-status http-msg (plist-get response :error)))
466+
((and-let* ((error-data
467+
(cond ((plistp response) (plist-get response :error))
468+
((arrayp response)
469+
(cl-some (lambda (el) (plist-get el :error)) response)))))
470+
(list nil http-status http-msg error-data)))
465471
((eq response 'json-read-error)
466472
(list nil http-status (concat "(" http-msg ") Malformed JSON in response.")
467473
"Malformed JSON in response"))

gptel.el

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,8 +3105,11 @@ See `gptel-curl--get-response' for its contents.")
31053105
((not (string-blank-p resp))))
31063106
(string-trim resp))
31073107
http-status http-msg))
3108-
((plist-get response :error)
3109-
(list nil http-status http-msg (plist-get response :error)))
3108+
((and-let* ((error-data
3109+
(cond ((plistp response) (plist-get response :error))
3110+
((arrayp response)
3111+
(cl-some (lambda (el) (plist-get el :error)) response)))))
3112+
(list nil http-status http-msg error-data)))
31103113
((eq response 'json-read-error)
31113114
(list nil http-status (concat "(" http-msg ") Malformed JSON in response.") "json-read-error"))
31123115
(t (list nil http-status (concat "(" http-msg ") Could not parse HTTP response.")

0 commit comments

Comments
 (0)