Repository navigation
Expand file tree
/
Copy patherror.go
More file actions
54 lines (47 loc) · 1.52 KB
/
Copy patherror.go
File metadata and controls
54 lines (47 loc) · 1.52 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
package tempmail
import (
"fmt"
"io"
"net/http"
)
// HTTPError reports one or more errors caused by an API request.
// Temp Mail API docs: https://docs.temp-mail.io/docs/getting-started#error-handling
type HTTPError struct {
Response *http.Response `json:"-"` // HTTP response that caused this error
ErrorDetails HTTPErrorError `json:"error"`
Meta HTTPErrorMeta `json:"meta"`
}
type HTTPErrorError struct {
// Type is the type of the error.
// Possible values: api_error, request_error.
Type string `json:"type"`
// Code is the error code.
Code string `json:"code"`
// Detail is the error message.
Detail string `json:"detail"`
}
type HTTPErrorMeta struct {
RequestID string `json:"request_id"`
}
func (h *HTTPError) Error() string {
return fmt.Sprintf("status %d, error type: %s, code: %s, detail: %s", h.Response.StatusCode, h.ErrorDetails.Type, h.ErrorDetails.Code, h.ErrorDetails.Detail)
}
func (h *HTTPError) fullError() string {
return fmt.Sprintf("status %d, error type: %s, code: %s, detail: %s, request_id: %s", h.Response.StatusCode, h.ErrorDetails.Type, h.ErrorDetails.Code, h.ErrorDetails.Detail, h.Meta.RequestID)
}
// Format implements fmt.Formatter interface.
// It adds request ID to the error message when called with %+v verb.
func (h *HTTPError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
_, _ = io.WriteString(s, h.fullError())
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, h.Error())
case 'q':
_, _ = fmt.Fprintf(s, "%q", h.Error())
}
}