forked from dstotijn/go-notion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
69 lines (59 loc) · 2.27 KB
/
error.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package notion
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// See: https://developers.notion.com/reference/errors.
var (
ErrInvalidJSON = errors.New("notion: request body could not be decoded as JSON")
ErrInvalidRequestURL = errors.New("notion: request URL is not valid")
ErrInvalidRequest = errors.New("notion: request is not supported")
ErrValidation = errors.New("notion: request body does not match the schema for the expected parameters")
ErrUnauthorized = errors.New("notion: bearer token is not valid")
ErrRestrictedResource = errors.New("notion: client doesn't have permission to perform this operation")
ErrObjectNotFound = errors.New("notion: the resource does not exist")
ErrConflict = errors.New("notion: the transaction could not be completed, potentially due to a data collision")
ErrRateLimited = errors.New("notion: this request exceeds the number of requests allowed")
ErrInternalServer = errors.New("notion: an unexpected error occurred")
ErrServiceUnavailable = errors.New("notion: service is unavailable")
)
var errMap = map[string]error{
"invalid_json": ErrInvalidJSON,
"invalid_request_url": ErrInvalidRequestURL,
"invalid_request": ErrInvalidRequest,
"validation_error": ErrValidation,
"unauthorized": ErrUnauthorized,
"restricted_resource": ErrRestrictedResource,
"object_not_found": ErrObjectNotFound,
"conflict_error": ErrConflict,
"rate_limited": ErrRateLimited,
"internal_server_error": ErrInternalServer,
"service_unavailable": ErrServiceUnavailable,
}
type APIError struct {
Object string `json:"object"`
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
}
// Error implements `error`.
func (err *APIError) Error() string {
return fmt.Sprintf("%v (code: %v, status: %v)", err.Message, err.Code, err.Status)
}
func (err *APIError) Unwrap() error {
mapped, ok := errMap[err.Code]
if !ok {
return fmt.Errorf("notion: %v", err.Error())
}
return mapped
}
func parseErrorResponse(res *http.Response) error {
var apiErr APIError
err := json.NewDecoder(res.Body).Decode(&apiErr)
if err != nil {
return fmt.Errorf("failed to parse error from HTTP response: %w", err)
}
return &apiErr
}