Skip to content

Commit a6082f7

Browse files
committed
Improve error handling
1 parent 56300ec commit a6082f7

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

src/eca/features/chat.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@
9292
:is-complete (boolean finish-reason)
9393
:content {:type :text
9494
:text message}}))
95-
:on-error (fn [e]
95+
:on-error (fn [{:keys [message exception]}]
9696
(messenger/chat-content-received
9797
messenger
9898
{:chat-id chat-id
9999
:request-id request-id
100100
:is-complete true
101101
:role :system
102102
:content {:type :text
103-
:text (str "\nError: " (ex-message e))}}))}))
103+
:text (or message (ex-message exception))}}))}))
104104
{:chat-id chat-id
105105
:status :success}))
106106

src/eca/llm_api.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
:api-key (:openai-api-key config)}
1717
{:on-message-received on-message-received
1818
:on-error on-error})
19-
(on-error (str "Unsupported model: " model))))
19+
(on-error {:msg (str "ECA Unsupported model: " model)})))

src/eca/llm_providers/openai.clj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
[cheshire.core :as json]
44
[clojure.java.io :as io]
55
[clojure.string :as str]
6+
[eca.logger :as logger]
67
[hato.client :as http]))
78

9+
(def ^:private logger-tag "[OPENAI]")
10+
811
(def ^:private url "https://api.openai.com/v1/chat/completions")
912

1013
(defn ^:private raw-data->messages [data]
@@ -29,12 +32,15 @@
2932
{:headers {"Authorization" (str "Bearer " api-key)
3033
"Content-Type" "application/json"}
3134
:body (json/generate-string body)
35+
:throw-exceptions? false
3236
:async? true
3337
:as :stream}
3438
(fn [{:keys [status body]}]
3539
(try
3640
(if (not= 200 status)
37-
(on-error status)
41+
(do
42+
(logger/warn logger-tag "Unexpected response status" status)
43+
(on-error {:message (str "OpenAI response status: " status)}))
3844
(with-open [rdr (io/reader body)]
3945
(doseq [line (line-seq rdr)]
4046
(when (str/starts-with? line "data: ")
@@ -43,7 +49,6 @@
4349
(doseq [message (raw-data->messages data)]
4450
(on-message-received message))))))))
4551
(catch Exception e
46-
;; TODO improve error handling
47-
(println "-->" e))))
52+
(on-error {:exception e}))))
4853
(fn [e]
49-
(on-error e)))))
54+
(on-error {:exception e})))))

src/eca/logger.clj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
(ns eca.logger)
22

3-
;; TODO create better logger capability.
4-
(defn info [& args]
3+
(defn ^:private stderr-print [& args]
54
(binding [*out* *err*]
65
(apply println args)))
76

7+
;; TODO create better logger capability.
8+
(defn info [& args]
9+
(apply stderr-print args))
10+
11+
(defn warn [& args]
12+
(apply stderr-print args))
13+
814
(defn format-time-delta-ms [start-time end-time]
915
(format "%.0fms" (float (/ (- end-time start-time) 1000000))))
1016

0 commit comments

Comments
 (0)