-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
42 lines (36 loc) · 861 Bytes
/
errors.go
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
package capis
import (
"errors"
"fmt"
"net/http"
)
type (
// ErrUnknown is returned when we don't know what the error is.
ErrUnknown struct {
statusCode int
}
)
var (
// ErrUnauthorized is returned when the token is invalid.
ErrUnauthorized = errors.New("unauthorized access, is the token valid")
// ErrNotFound is returned when the resource is not found.
ErrNotFound = errors.New("not found")
// ErrUnreachable is returned when we cannot connect to the capis server.
ErrUnreachable = errors.New("unreachable")
)
func statusCodeToError(sc int) error {
if sc < 400 {
return nil
}
switch sc {
case http.StatusUnauthorized:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return &ErrUnknown{sc}
}
}
func (e *ErrUnknown) Error() string {
return fmt.Sprintf("unknown status code: %d", e.statusCode)
}