From 36051b15d5ccb1ad7940a0449cace3edaefd09e2 Mon Sep 17 00:00:00 2001 From: Karthik Chikmagalur Date: Sat, 8 Apr 2023 12:22:36 -0700 Subject: [PATCH] gptel: Unify error descriptions * gptel.el (gptel--url-parse-response, gptel--insert-response): Use the same error codes/descriptions across url-retrieve/Curl, with and without streaming responses. * gptel-curl.el (gptel-curl--parse-response, gptel-curl--stream-filter, gptel-curl--stream-cleanup): Ditto. --- gptel-curl.el | 28 ++++++++++++++------------ gptel.el | 56 ++++++++++++++++++++++++++------------------------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/gptel-curl.el b/gptel-curl.el index 885854a..83aef29 100644 --- a/gptel-curl.el +++ b/gptel-curl.el @@ -126,6 +126,7 @@ PROCESS and STATUS are process parameters." (search-backward (plist-get info :token)) (backward-char) (pcase-let* ((`(,_ . ,header-size) (read (current-buffer))) + (json-object-type 'plist) (response (progn (goto-char header-size) (condition-case nil (json-read) (json-readtable-error 'json-read-error))))) @@ -134,8 +135,8 @@ PROCESS and STATUS are process parameters." (let* ((error-plist (plist-get response :error)) (error-msg (plist-get error-plist :message)) (error-type (plist-get error-plist :type))) - (message "ChatGPT error: %s" error-msg) - (setq http-msg (concat http-msg ": " (string-trim error-type))))) + (message "ChatGPT error: (%s) %s" http-msg error-msg) + (setq http-msg (concat "(" http-msg ") " (string-trim error-type))))) ((eq response 'json-read-error) (message "ChatGPT error (%s): Malformed JSON in response." http-msg)) (t (message "ChatGPT error (%s): Could not parse HTTP response." http-msg))))) @@ -200,7 +201,7 @@ See `gptel--url-get-response' for details." (and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg) (match-string 1 http-msg))))) (plist-put proc-info :http-status http-status) - (plist-put proc-info :http-msg http-msg)))) + (plist-put proc-info :http-msg (string-trim http-msg))))) (when-let ((http-msg (plist-get proc-info :http-msg)) (http-status (plist-get proc-info :http-status))) @@ -259,8 +260,9 @@ buffer." ;; (goto-char (point-min))) (goto-char (point-min)) - (if-let* ((http-msg (buffer-substring (line-beginning-position) - (line-end-position))) + (if-let* ((http-msg (string-trim + (buffer-substring (line-beginning-position) + (line-end-position)))) (http-status (save-match-data (and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg) @@ -280,16 +282,16 @@ buffer." (let* ((error-plist (plist-get response :error)) (error-msg (plist-get error-plist :message)) (error-type (plist-get error-plist :type))) - (message "ChatGPT error: %s" error-msg) - (list :content nil :status (concat http-msg ": " error-type)))) + (message "ChatGPT error: (%s) %s" http-msg error-msg) + (list :content nil :status (concat "(" http-msg ") " (string-trim error-type))))) ((eq response 'json-read-error) - (message "ChatGPT error: Malformed JSON in response.") - (list :content nil :status (concat http-msg ": Malformed JSON in response."))) - (t (message "ChatGPT error: Could not parse HTTP response.") - (list :content nil :status (concat http-msg ": Could not parse HTTP response.")))) - (message "ChatGPT error: Could not parse HTTP response.") + (message "ChatGPT error: (%s) Malformed JSON in response." http-msg) + (list :content nil :status (concat "(" http-msg ") Malformed JSON in response."))) + (t (message "ChatGPT error (%s): Could not parse HTTP response." http-msg) + (list :content nil :status (concat "(" http-msg ") Could not parse HTTP response.")))) + (message "ChatGPT error: (%s) Could not parse HTTP response." http-msg) (list :content nil - :status (concat http-msg ": Could not parse HTTP response."))))))) + :status (concat "(" http-msg ") Could not parse HTTP response."))))))) (provide 'gptel-curl) ;;; gptel-curl.el ends here diff --git a/gptel.el b/gptel.el index c830a71..d03c263 100644 --- a/gptel.el +++ b/gptel.el @@ -317,24 +317,25 @@ See `gptel--url-get-response' for details." (status-str (plist-get response :status)) (gptel-buffer (plist-get info :gptel-buffer)) (start-marker (plist-get info :start-marker))) - (if content-str - (with-current-buffer gptel-buffer - (setq content-str (gptel--transform-response - content-str gptel-buffer)) - (save-excursion - (put-text-property 0 (length content-str) 'gptel 'response content-str) - (message "Querying ChatGPT... done.") - (goto-char start-marker) - (unless (bobp) (insert "\n\n")) - (let ((p (point))) - (insert content-str) - (pulse-momentary-highlight-region p (point))) - (when gptel-mode + (with-current-buffer gptel-buffer + (if content-str + (progn + (setq content-str (gptel--transform-response + content-str gptel-buffer)) + (save-excursion + (put-text-property 0 (length content-str) 'gptel 'response content-str) + (message "Querying ChatGPT... done.") + (goto-char start-marker) + (unless (bobp) (insert "\n\n")) + (let ((p (point))) + (insert content-str) + (pulse-momentary-highlight-region p (point))) + (when gptel-mode (insert "\n\n" (gptel-prompt-string)) (gptel--update-header-line " Ready" 'success)))) - (gptel--update-header-line - (format " Response Error: %s" status-str) 'error)) - (run-hooks 'gptel-post-response-hook))) + (gptel--update-header-line + (format " Response Error: %s" status-str) 'error)) + (run-hooks 'gptel-post-response-hook)))) (defun gptel--create-prompt (&optional prompt-end) "Return a full conversation prompt from the contents of this buffer. @@ -452,7 +453,8 @@ the response is inserted into the current buffer after point." (with-current-buffer response-buffer (clone-buffer "*gptel-error*" 'show))) (with-current-buffer response-buffer - (if-let* ((status (buffer-substring (line-beginning-position) (line-end-position))) + (if-let* ((http-msg (string-trim (buffer-substring (line-beginning-position) + (line-end-position)))) (json-object-type 'plist) (response (progn (forward-paragraph) (let ((json-str (decode-coding-string @@ -462,23 +464,23 @@ the response is inserted into the current buffer after point." (json-read-from-string json-str) (json-readtable-error 'json-read-error)))))) (cond - ((string-match-p "200 OK" status) + ((string-match-p "200 OK" http-msg) (list :content (string-trim (map-nested-elt response '(:choices 0 :message :content))) - :status status)) + :status http-msg)) ((plist-get response :error) (let* ((error-plist (plist-get response :error)) (error-msg (plist-get error-plist :message)) (error-type (plist-get error-plist :type))) - (message "ChatGPT error: %s" error-msg) - (list :content nil :status (concat status ": " error-type)))) + (message "ChatGPT error: (%s) %s" http-msg error-msg) + (list :content nil :status (concat "(" http-msg ") " error-type)))) ((eq response 'json-read-error) - (message "ChatGPT error: Malformed JSON in response.") - (list :content nil :status (concat status ": Malformed JSON in response."))) - (t (message "ChatGPT error: Could not parse HTTP response.") - (list :content nil :status (concat status ": Could not parse HTTP response.")))) - (message "ChatGPT error: Could not parse HTTP response.") + (message "ChatGPT error: (%s) Malformed JSON in response." http-msg) + (list :content nil :status (concat http-msg ": Malformed JSON in response."))) + (t (message "ChatGPT error: (%s) Could not parse HTTP response." http-msg) + (list :content nil :status (concat "(" http-msg ") Could not parse HTTP response.")))) + (message "ChatGPT error: (%s) Could not parse HTTP response." http-msg) (list :content nil - :status (concat status ": Could not parse HTTP response.")))))) + :status (concat "(" http-msg ") Could not parse HTTP response.")))))) ;;;###autoload (defun gptel (name &optional api-key initial)