forked from nscuro/dtrack-client
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy patherror.go
More file actions
34 lines (27 loc) · 596 Bytes
/
error.go
File metadata and controls
34 lines (27 loc) · 596 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
package dtrack
import (
"fmt"
"io"
"net/http"
)
type APIError struct {
StatusCode int
Message string
}
func (e APIError) Error() string {
if e.Message == "" {
return fmt.Sprintf("api error (status: %d)", e.StatusCode)
}
return fmt.Sprintf("%s (status: %d)", e.Message, e.StatusCode)
}
func checkResponseForError(res *http.Response) error {
if res.StatusCode >= 200 && res.StatusCode < 300 {
return nil
}
apiErr := &APIError{StatusCode: res.StatusCode}
body, err := io.ReadAll(res.Body)
if err == nil && body != nil {
apiErr.Message = string(body)
}
return apiErr
}