adding detailed error handling to the omnivore integration

This commit is contained in:
Jesse Jaggars 2023-12-05 10:31:00 -05:00 committed by Frédéric Guillot
parent e933fb11e9
commit 95039410b5
1 changed files with 30 additions and 5 deletions

View File

@ -41,6 +41,21 @@ type SaveUrlInput struct {
Url string `json:"url"`
}
type errorResponse struct {
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
type successResponse struct {
Data struct {
SaveUrl struct {
Url string `json:"url"`
ClientRequestId string `json:"clientRequestId"`
} `json:"saveUrl"`
} `json:"data"`
}
type Client interface {
SaveUrl(url string) error
}
@ -88,13 +103,23 @@ func (c *client) SaveUrl(url string) error {
return err
}
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("omnivore: failed to parse response: %s", err)
}
if resp.StatusCode >= 400 {
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
if err != nil {
return err
var errResponse errorResponse
if err = json.Unmarshal(b, &errResponse); err != nil {
return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, string(b))
}
return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, string(b))
return fmt.Errorf("omnivore: failed to save URL: status=%d %s", resp.StatusCode, errResponse.Errors[0].Message)
}
var successReponse successResponse
if err = json.Unmarshal(b, &successReponse); err != nil {
return fmt.Errorf("omnivore: failed to parse response, however the request appears successful, is the url correct?: status=%d %s", resp.StatusCode, string(b))
}
return nil