branch: externals/llm commit 3441784ae250c5b4c93470f3adb9eed1fc149b2e Author: Roman Scherer <ro...@burningswell.com> Commit: Roman Scherer <ro...@burningswell.com>
Fix error handling. The on-error callback should be called with 2 arguments, a code and the payload. There are 2 cases: HTTP errors do have a status code and response. In this case we call the error handler with the HTTP status code and the HTTP body. Curl error don't have a response. These are errors like DNS errors, connection timouts etc. In those cases we call the error handler with the curl exit code, and an error message (derived from that code). --- llm-request-plz.el | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/llm-request-plz.el b/llm-request-plz.el index 22b8d63aac..03eb6ae1cc 100644 --- a/llm-request-plz.el +++ b/llm-request-plz.el @@ -93,7 +93,27 @@ TIMEOUT is the number of seconds to wait for a response." :data data :timeout timeout)) -(cl-defun llm-request-plz-async (url &key headers data on-success on-success-raw on-error +(defun llm-request-plz--handle-error (error on-error) + "Handle the ERROR with the ON-ERROR callback. + +For HTTP errors, ON-ERROR will be called with the HTTP status +code and the HTTP body of the error response. + +For Curl errors, ON-ERROR will be called with the exit code of +the curl process and an error message." + (cond ((plz-error-response error) + (let ((response (plz-error-response error))) + (funcall on-error + (plz-response-status response) + (plz-response-body response)))) + ((plz-error-curl-error error) + (let ((curl-error (plz-error-curl-error error))) + (funcall on-error + (car curl-error) + (cdr curl-error)))) + (t (user-error "Unexpected error: %s" error)))) + +(cl-defun llm-request-plz-async (url &key headers data on-success on-success-raw on-error on-partial timeout) "Make a request to URL. Nothing will be returned. @@ -133,7 +153,7 @@ the buffer is turned into JSON and passed to ON-SUCCESS." (funcall on-success (json-read-from-string response)))) :else (lambda (error) (when on-error - (funcall on-error error))) + (llm-request-plz--handle-error error on-error))) :timeout (or timeout llm-request-plz-timeout))) (cl-defun llm-request-plz-event-stream (url &key headers data on-error on-success @@ -182,7 +202,7 @@ This is required. (funcall on-success (plz-response-body response)))) :else (lambda (error) (when on-error - (funcall on-error error))) + (llm-request-plz--handle-error error on-error))) :timeout (or timeout llm-request-plz-timeout))) ;; This is a useful method for getting out of the request buffer when it's time