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

View file

@ -41,6 +41,21 @@ type SaveUrlInput struct {
Url string `json:"url"` 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 { type Client interface {
SaveUrl(url string) error SaveUrl(url string) error
} }
@ -88,14 +103,24 @@ func (c *client) SaveUrl(url string) error {
return err return err
} }
if resp.StatusCode >= 400 {
defer resp.Body.Close() defer resp.Body.Close()
b, err = io.ReadAll(resp.Body) b, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return fmt.Errorf("omnivore: failed to parse response: %s", err)
} }
if resp.StatusCode >= 400 {
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 return nil
} }