-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#170: moved error handling to a sep component
- Loading branch information
1 parent
58ef8f2
commit 29c38d4
Showing
10 changed files
with
179 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package anthropic | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"glide/pkg/providers/clients" | ||
"glide/pkg/telemetry" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ErrorMapper struct { | ||
tel *telemetry.Telemetry | ||
} | ||
|
||
func NewErrorMapper(tel *telemetry.Telemetry) *ErrorMapper { | ||
return &ErrorMapper{ | ||
tel: tel, | ||
} | ||
} | ||
|
||
func (m *ErrorMapper) Map(resp *http.Response) error { | ||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
m.tel.Logger.Error("failed to read anthropic chat response", zap.Error(err)) | ||
} | ||
|
||
m.tel.Logger.Error( | ||
"anthropic chat request failed", | ||
zap.Int("status_code", resp.StatusCode), | ||
zap.String("response", string(bodyBytes)), | ||
zap.Any("headers", resp.Header), | ||
) | ||
|
||
if resp.StatusCode == http.StatusTooManyRequests { | ||
// Read the value of the "Retry-After" header to get the cooldown delay | ||
retryAfter := resp.Header.Get("Retry-After") | ||
|
||
// Parse the value to get the duration | ||
cooldownDelay, err := time.ParseDuration(retryAfter) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse cooldown delay from headers: %w", err) | ||
} | ||
|
||
return clients.NewRateLimitError(&cooldownDelay) | ||
} | ||
|
||
if resp.StatusCode == http.StatusUnauthorized { | ||
return clients.ErrUnauthorized | ||
} | ||
|
||
// Server & client errors result in the same error to keep gateway resilient | ||
return clients.ErrProviderUnavailable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package azureopenai | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"glide/pkg/providers/clients" | ||
"glide/pkg/telemetry" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ErrorMapper struct { | ||
tel *telemetry.Telemetry | ||
} | ||
|
||
func NewErrorMapper(tel *telemetry.Telemetry) *ErrorMapper { | ||
return &ErrorMapper{ | ||
tel: tel, | ||
} | ||
} | ||
|
||
func (m *ErrorMapper) Map(resp *http.Response) error { | ||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
m.tel.L().Error("failed to read azure openai chat response", zap.Error(err)) | ||
} | ||
|
||
m.tel.L().Error( | ||
"azure openai chat request failed", | ||
zap.Int("status_code", resp.StatusCode), | ||
zap.String("response", string(bodyBytes)), | ||
zap.Any("headers", resp.Header), | ||
) | ||
|
||
if resp.StatusCode == http.StatusTooManyRequests { | ||
// Read the value of the "Retry-After" header to get the cooldown delay | ||
retryAfter := resp.Header.Get("Retry-After") | ||
|
||
// Parse the value to get the duration | ||
cooldownDelay, err := time.ParseDuration(retryAfter) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse cooldown delay from headers: %w", err) | ||
} | ||
|
||
return clients.NewRateLimitError(&cooldownDelay) | ||
} | ||
|
||
if resp.StatusCode == http.StatusUnauthorized { | ||
return clients.ErrUnauthorized | ||
} | ||
|
||
// Server & client errors result in the same error to keep gateway resilient | ||
return clients.ErrProviderUnavailable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package octoml | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"glide/pkg/providers/clients" | ||
"glide/pkg/telemetry" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ErrorMapper struct { | ||
tel *telemetry.Telemetry | ||
} | ||
|
||
func NewErrorMapper(tel *telemetry.Telemetry) *ErrorMapper { | ||
return &ErrorMapper{ | ||
tel: tel, | ||
} | ||
} | ||
|
||
func (m *ErrorMapper) Map(resp *http.Response) error { | ||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
m.tel.L().Error("failed to read octoml chat response", zap.Error(err)) | ||
} | ||
|
||
m.tel.L().Error( | ||
"octoml chat request failed", | ||
zap.Int("status_code", resp.StatusCode), | ||
zap.String("response", string(bodyBytes)), | ||
zap.Any("headers", resp.Header), | ||
) | ||
|
||
if resp.StatusCode == http.StatusTooManyRequests { | ||
// Read the value of the "Retry-After" header to get the cooldown delay | ||
retryAfter := resp.Header.Get("Retry-After") | ||
|
||
// Parse the value to get the duration | ||
cooldownDelay, err := time.ParseDuration(retryAfter) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse cooldown delay from headers: %w", err) | ||
} | ||
|
||
return clients.NewRateLimitError(&cooldownDelay) | ||
} | ||
|
||
if resp.StatusCode == http.StatusUnauthorized { | ||
return clients.ErrUnauthorized | ||
} | ||
|
||
// Server & client errors result in the same error to keep gateway resilient | ||
return clients.ErrProviderUnavailable | ||
} |