branch: elpa/gptel commit 0ff882cf99924ef1949d882c50df928518373263 Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
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. --- gptel-curl.el | 12 +++++++++--- gptel.el | 7 +++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/gptel-curl.el b/gptel-curl.el index beff5dc796..018ff07a95 100644 --- a/gptel-curl.el +++ b/gptel-curl.el @@ -241,7 +241,10 @@ PROCESS and _STATUS are process parameters." (response (progn (goto-char header-size) (condition-case nil (gptel--json-read) (error 'json-read-error)))) - (error-data (plist-get response :error))) + (error-data + (cond ((plistp response) (plist-get response :error)) + ((arrayp response) + (cl-some (lambda (el) (plist-get el :error)) response))))) (cond (error-data (plist-put info :error error-data)) @@ -460,8 +463,11 @@ PROC-INFO is a plist with contextual information." ((not (string-blank-p resp)))) (string-trim resp)) http-status http-msg)) - ((plist-get response :error) - (list nil http-status http-msg (plist-get response :error))) + ((and-let* ((error-data + (cond ((plistp response) (plist-get response :error)) + ((arrayp response) + (cl-some (lambda (el) (plist-get el :error)) response))))) + (list nil http-status http-msg error-data))) ((eq response 'json-read-error) (list nil http-status (concat "(" http-msg ") Malformed JSON in response.") "Malformed JSON in response")) diff --git a/gptel.el b/gptel.el index 947d996f4f..4f70524e09 100644 --- a/gptel.el +++ b/gptel.el @@ -3105,8 +3105,11 @@ See `gptel-curl--get-response' for its contents.") ((not (string-blank-p resp)))) (string-trim resp)) http-status http-msg)) - ((plist-get response :error) - (list nil http-status http-msg (plist-get response :error))) + ((and-let* ((error-data + (cond ((plistp response) (plist-get response :error)) + ((arrayp response) + (cl-some (lambda (el) (plist-get el :error)) response))))) + (list nil http-status http-msg error-data))) ((eq response 'json-read-error) (list nil http-status (concat "(" http-msg ") Malformed JSON in response.") "json-read-error")) (t (list nil http-status (concat "(" http-msg ") Could not parse HTTP response.")