Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sebv committed Jan 24, 2024
1 parent eec779b commit e166ab0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions internal/http/imagerunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,12 @@ func (c *ImageRunner) OpenAsyncEventsWebsocket(ctx context.Context, id string, l
EnableCompression: true,
}
ws, resp, err := dialer.Dial(url, headers)
if resp.StatusCode == http.StatusUnauthorized {
return nil, imagerunner.NotAuthorizedError{}
if resp.StatusCode == http.StatusNotFound ||
resp.StatusCode == http.StatusUnauthorized ||
resp.StatusCode == http.StatusForbidden {
return nil, imagerunner.AsyncEventFatalError{
Err: errors.New(resp.Status),
}
}
if err != nil {
return nil, err
Expand All @@ -291,7 +295,7 @@ func (c *ImageRunner) OpenAsyncEventsWebsocket(ctx context.Context, id string, l
func (c *ImageRunner) OpenAsyncEventsTransport(ctx context.Context, id string, lastseq string, nowait bool) (imagerunner.AsyncEventTransportI, error) {
ws, err := c.OpenAsyncEventsWebsocket(ctx, id, lastseq, nowait)
if err != nil {
if _, ok := err.(imagerunner.NotAuthorizedError); ok {
if _, ok := err.(imagerunner.AsyncEventFatalError); ok {
return nil, err
}
return nil, imagerunner.AsyncEventSetupError{
Expand All @@ -317,7 +321,7 @@ func (c *ImageRunner) HandleAsyncEvents(ctx context.Context, id string, nowait b
if errors.Is(err, context.Canceled) {
return err
}
if _, ok := err.(imagerunner.NotAuthorizedError); ok {
if _, ok := err.(imagerunner.AsyncEventFatalError); ok {
return err
}
if !hasMoreLines {
Expand Down
7 changes: 4 additions & 3 deletions internal/imagerunner/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ func (e AsyncEventSetupError) Error() string {
return fmt.Sprintf("streaming setup failed with: %v", e.Err)
}

type NotAuthorizedError struct {
type AsyncEventFatalError struct {
Err error
}

func (e NotAuthorizedError) Error() string {
return "not authorized"
func (e AsyncEventFatalError) Error() string {
return e.Err.Error()
}

0 comments on commit e166ab0

Please sign in to comment.