Skip to content

Commit

Permalink
fix: return cobalt api error
Browse files Browse the repository at this point in the history
  • Loading branch information
andresperezl committed Sep 8, 2024
1 parent b4660d5 commit ce6b432
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (c *Cobalt) Get(ctx context.Context, params Request) (*Media, error) {
return nil, err
}

url := fmt.Sprintf("%s%s", c.apiBaseURL, EndpointJSON)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buff)
u := fmt.Sprintf("%s%s", c.apiBaseURL, EndpointJSON)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, buff)
if err != nil {
return nil, err
}
Expand All @@ -71,6 +71,10 @@ func (c *Cobalt) Get(ctx context.Context, params Request) (*Media, error) {
return nil, err
}

if media.Status == ResponseStatusError {
return nil, CobaltAPIError(*media)
}

return media, nil
}

Expand Down Expand Up @@ -139,3 +143,10 @@ func (m *Media) Stream(ctx context.Context) (io.ReadCloser, error) {

return resp.Body, nil
}

// CobalAPIError is just a convenient type to convert Media into an error.
type CobaltAPIError Media

func (err CobaltAPIError) Error() string {
return err.Text
}

0 comments on commit ce6b432

Please sign in to comment.