-
Notifications
You must be signed in to change notification settings - Fork 3
/
errors.go
76 lines (63 loc) · 1.81 KB
/
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
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
70
71
72
73
74
75
76
// Copyright (c) 2022, John Moore
// All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package clickup
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
var ErrValidation = errors.New("invalid input provided")
type RateLimitError struct {
msg string
limit string
remaining string
resetAt string
cause error
}
func (r *RateLimitError) Error() string {
return fmt.Sprintf("%s - limit: %s, remaining: %s, reset at: %s", r.msg, r.limit, r.remaining, r.resetAt)
}
func (r *RateLimitError) Unwrap() error {
return r.cause
}
type HTTPError struct {
Status string
StatusCode int
URL string
}
func (h *HTTPError) Error() string {
return fmt.Sprintf("clickup response [%s] status: %s code: %d", h.URL, h.Status, h.StatusCode)
}
type ErrClickupResponse struct {
ECode string `json:"ECODE"`
Err string `json:"err"`
StatusCode int
Status string
}
func (e *ErrClickupResponse) Error() string {
return fmt.Sprintf("clickup response ECODE=%s Err=%s StatusCode=%d Status=%s", e.ECode, e.Err, e.StatusCode, e.Status)
}
func errorFromResponse(res *http.Response, decoder *json.Decoder) error {
if res.StatusCode == http.StatusTooManyRequests {
return &RateLimitError{
msg: "rate limit exceeded",
limit: res.Header.Get("x-ratelimit-limit"),
remaining: res.Header.Get("x-ratelimit-remaining"),
resetAt: res.Header.Get("x-ratelimit-reset"),
}
}
var errResponse ErrClickupResponse
if err := decoder.Decode(&errResponse); err != nil {
return &HTTPError{
Status: res.Status,
StatusCode: res.StatusCode,
URL: res.Request.URL.String(),
}
}
errResponse.StatusCode = res.StatusCode
errResponse.Status = res.Status
return &errResponse
}