Skip to content

Commit 528358a

Browse files
committed
improve error handling
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
1 parent 0810138 commit 528358a

File tree

4 files changed

+8
-73
lines changed

4 files changed

+8
-73
lines changed

aibridged/aibridged.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ func (s *Server) BridgeAddr() string {
254254
return s.bridge.Addr()
255255
}
256256

257-
func (s *Server) BridgeErr() error {
258-
return s.bridge.lastErr
259-
}
260-
261257
// TODO: direct copy/paste from provisionerd, abstract into common util.
262258
func retryable(err error) bool {
263259
return xerrors.Is(err, yamux.ErrSessionShutdown) || xerrors.Is(err, io.EOF) || xerrors.Is(err, fasthttputil.ErrInmemoryListenerClosed) ||

aibridged/bridge.go

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
132132
b.logger.Info(r.Context(), "openai request started", slog.F("session_id", sessionID), slog.F("method", r.Method), slog.F("path", r.URL.Path))
133133
_, _ = fmt.Fprintf(os.Stderr, "[%s] new chat session started\n\n", sessionID)
134134

135-
// Clear any previous error state
136-
b.clearError()
137-
138135
defer func() {
139136
b.logger.Info(r.Context(), "openai request ended", slog.F("session_id", sessionID))
140137
_, _ = fmt.Fprintf(os.Stderr, "[%s] chat session ended\n\n", sessionID)
@@ -321,15 +318,17 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
321318
}
322319

323320
if err := stream.Err(); err != nil {
321+
b.logger.Error(ctx, "server stream error", slog.Error(err))
324322
var apierr *openai.Error
325323
if errors.As(err, &apierr) {
326-
http.Error(w, apierr.Message, apierr.StatusCode)
327-
return
324+
events.TrySend(ctx, map[string]interface{}{
325+
"error": true,
326+
"message": err.Error(),
327+
})
328+
// http.Error(w, apierr.Message, apierr.StatusCode)
329+
break
328330
} else if isConnectionError(err) {
329-
b.logger.Debug(ctx, "upstream connection closed", slog.Error(err))
330-
} else {
331-
b.logger.Error(ctx, "server stream error", slog.Error(err))
332-
b.setError(err)
331+
b.logger.Warn(ctx, "upstream connection error", slog.Error(err))
333332
}
334333

335334
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -433,7 +432,6 @@ func (b *Bridge) proxyOpenAIRequest(w http.ResponseWriter, r *http.Request) {
433432
}
434433

435434
b.logger.Error(ctx, "chat completion failed", slog.Error(err))
436-
b.setError(err)
437435
http.Error(w, err.Error(), http.StatusInternalServerError)
438436
return
439437
}
@@ -582,9 +580,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
582580
b.logger.Info(r.Context(), "anthropic request started", slog.F("session_id", sessionID), slog.F("method", r.Method), slog.F("path", r.URL.Path))
583581
_, _ = fmt.Fprintf(os.Stderr, "[%s] new chat session started\n\n", sessionID)
584582

585-
// Clear any previous error state
586-
b.clearError()
587-
588583
defer func() {
589584
b.logger.Info(r.Context(), "anthropic request ended", slog.F("session_id", sessionID))
590585
_, _ = fmt.Fprintf(os.Stderr, "[%s] chat session ended\n\n", sessionID)
@@ -633,7 +628,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
633628
// Policy examples.
634629
if strings.Contains(string(in.Model), "opus") {
635630
err := xerrors.Errorf("%q model is not allowed", in.Model)
636-
b.setError(err)
637631
http.Error(w, err.Error(), http.StatusBadRequest)
638632
return
639633
}
@@ -1163,10 +1157,6 @@ func (b *Bridge) proxyAnthropicRequest(w http.ResponseWriter, r *http.Request) {
11631157
if err != nil {
11641158
b.logger.Error(ctx, "failed to send error", slog.Error(err))
11651159
}
1166-
1167-
b.setError(antErr)
1168-
} else {
1169-
b.setError(streamErr)
11701160
}
11711161
}
11721162

@@ -1258,45 +1248,6 @@ func (b *Bridge) Addr() string {
12581248
return b.addr
12591249
}
12601250

1261-
// setError sets a structured error with appropriate context
1262-
func (b *Bridge) setError(val any) {
1263-
switch err := val.(type) {
1264-
case error:
1265-
switch {
1266-
case errors.Is(err, context.Canceled):
1267-
b.lastErr = &BridgeError{
1268-
Code: ErrorTypeRequestCanceled,
1269-
Message: "Request was canceled",
1270-
StatusCode: http.StatusRequestTimeout,
1271-
}
1272-
case isConnectionError(err):
1273-
b.lastErr = &BridgeError{
1274-
Code: ErrorTypeConnectionError,
1275-
Message: "Connection to upstream service failed",
1276-
StatusCode: http.StatusBadGateway,
1277-
}
1278-
default:
1279-
b.lastErr = &BridgeError{
1280-
Code: ErrorTypeUnexpectedError,
1281-
Message: err.Error(),
1282-
StatusCode: http.StatusInternalServerError,
1283-
}
1284-
}
1285-
case AnthropicErrorResponse:
1286-
b.lastErr = &BridgeError{
1287-
Code: ErrorTypeAnthropicAPIError,
1288-
Message: err.Error.Message,
1289-
Details: map[string]string{"type": err.Error.Type},
1290-
StatusCode: err.StatusCode,
1291-
}
1292-
}
1293-
}
1294-
1295-
// clearError clears the error state when a new request starts
1296-
func (b *Bridge) clearError() {
1297-
b.lastErr = nil
1298-
}
1299-
13001251
// logConnectionError logs connection errors with appropriate severity
13011252
func (b *Bridge) logConnectionError(ctx context.Context, err error, operation string) {
13021253
if isConnectionError(err) {

aibridged/constants.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

coderd/aibridge.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ func (r *rt) RoundTrip(req *http.Request) (*http.Response, error) {
2727

2828
resp, err := r.RoundTripper.RoundTrip(req)
2929

30-
if err != nil || resp.StatusCode == aibridged.ProxyErrCode {
31-
lastErr := r.server.BridgeErr()
32-
if lastErr != nil {
33-
return resp, lastErr
34-
}
35-
}
36-
3730
return resp, err
3831
}
3932

0 commit comments

Comments
 (0)