forked from peterhellberg/fixer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
57 lines (50 loc) · 1.27 KB
/
errors.go
File metadata and controls
57 lines (50 loc) · 1.27 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package fixer
import (
"fmt"
"io"
"net/http"
)
// NewError creates a new Error
func NewError(msg string) *Error {
return &Error{msg: msg}
}
// Error type for Fixer API requests
type Error struct {
msg string
}
// Error message
func (e *Error) Error() string {
return e.msg
}
// Errors
var (
ErrNilResponse = NewError("Unexpected nil response")
ErrUnexpectedStatus = NewError("Unexpected status")
ErrNotFound = NewError(http.StatusText(http.StatusNotFound))
ErrUnprocessableEntity = NewError(http.StatusText(http.StatusUnprocessableEntity))
ErrUnauthorized = NewError(http.StatusText(http.StatusUnauthorized))
ErrInternalServerError = NewError(http.StatusText(http.StatusInternalServerError))
)
func responseError(resp *http.Response) error {
if resp == nil {
return ErrNilResponse
}
switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
return ErrNotFound
case http.StatusUnauthorized:
return ErrUnauthorized
case http.StatusUnprocessableEntity:
return ErrUnprocessableEntity
case http.StatusInternalServerError:
return ErrInternalServerError
default:
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
return fmt.Errorf("Response Error: %s: %s", resp.Status, string(body))
}
}