Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/yetibot/core/util/xai.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@
(error "xai: failed to optimize prompt:" (.getMessage e))
prompt))))

(defn- extract-api-error
"Extract a human-readable error message from an xAI API error response body."
[body status]
(try
(let [parsed (if (string? body)
(json/read-str body :key-fn keyword)
body)]
(if (map? parsed)
(let [err (:error parsed)]
(cond
(map? err) (or (:message err) (str "HTTP error " status))
(string? err) err
(string? (:message parsed)) (:message parsed)
(string? (:detail parsed)) (:detail parsed)
:else (str "HTTP error " status)))
(str "HTTP error " status)))
(catch Exception _
(str "HTTP error " status))))

(defn generate-image
"Call the xAI API to generate an image from a text prompt.
If image-urls is provided and non-empty, performs image editing via the /v1/images/edits endpoint."
Expand Down Expand Up @@ -82,8 +101,7 @@
:mime-type "image/jpeg"}
(throw (ex-info "No image data returned from xAI API."
{:response-body (:body response)})))
(let [error-msg (or (get-in (:body response) [:error :message])
(str "HTTP error " status))]
(let [error-msg (extract-api-error (:body response) status)]
(error "xai: API error" status "-" error-msg)
(throw (ex-info (str "xAI API error: " error-msg)
{:type :xai-api-error
Expand Down Expand Up @@ -115,8 +133,7 @@
{:text text :cost cost})
(throw (ex-info "No chat completion content returned from xAI API."
{:response-body (:body response)})))
(let [error-msg (or (get-in (:body response) [:error :message])
(str "HTTP error " status))]
(let [error-msg (extract-api-error (:body response) status)]
(error "xai: API error" status "-" error-msg)
(throw (ex-info (str "xAI API error: " error-msg)
{:type :xai-api-error
Expand Down
40 changes: 40 additions & 0 deletions test/yetibot/core/test/util/xai_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,46 @@
=> {:status 429
:body {:error {:message "Rate limit exceeded"}}})))

(fact "it throws an error when API returns flat error message (non-nested)"
(with-redefs [xai/config {:key "secret-key"}
xai/optimize-image-prompt identity]
(xai/generate-image "a green banana") => (throws Exception #"xAI API error: Argument not supported")
(provided
(client/post "https://api.x.ai/v1/images/generations"
(contains {:headers {"Authorization" "Bearer secret-key"}
:content-type :json
:as :json
:body string?}))
=> {:status 400
:body {:code "invalid_request_error"
:error "Argument not supported"}})))

(fact "it throws an error when API returns flat error message as a raw JSON string"
(with-redefs [xai/config {:key "secret-key"}
xai/optimize-image-prompt identity]
(xai/generate-image "a green banana") => (throws Exception #"xAI API error: Custom JSON string error")
(provided
(client/post "https://api.x.ai/v1/images/generations"
(contains {:headers {"Authorization" "Bearer secret-key"}
:content-type :json
:as :json
:body string?}))
=> {:status 400
:body "{\"error\": \"Custom JSON string error\"}"})))

(fact "it falls back to HTTP error status code when API response is unparseable"
(with-redefs [xai/config {:key "secret-key"}
xai/optimize-image-prompt identity]
(xai/generate-image "a green banana") => (throws Exception #"xAI API error: HTTP error 400")
(provided
(client/post "https://api.x.ai/v1/images/generations"
(contains {:headers {"Authorization" "Bearer secret-key"}
:content-type :json
:as :json
:body string?}))
=> {:status 400
:body "Bad Request"})))

(fact "it throws an error when API response is missing image data"
(with-redefs [xai/config {:key "secret-key"}
xai/optimize-image-prompt identity]
Expand Down