forked from mehanizm/airtable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
38 lines (31 loc) · 913 Bytes
/
errors.go
File metadata and controls
38 lines (31 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"fmt"
"io/ioutil"
"net/http"
)
// HTTPClientError custom error to handle with response status.
type HTTPClientError struct {
StatusCode int
Err error
}
func (e *HTTPClientError) Error() string {
return fmt.Sprintf("status %d, err: %v", e.StatusCode, e.Err)
}
func makeHTTPClientError(url string, resp *http.Response) error {
var resError error
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
resError = fmt.Errorf("HTTP request failure on %s with status %d\nCannot parse body with: %w", url, resp.StatusCode, err)
} else {
resError = fmt.Errorf("HTTP request failure on %s with status %d\nBody: %v", url, resp.StatusCode, string(body))
}
return &HTTPClientError{
StatusCode: resp.StatusCode,
Err: resError,
}
}