|
| 1 | +package temp_mail_go |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | +) |
| 7 | + |
| 8 | +// HTTPError is the error response that will be returned by the API. |
| 9 | +type HTTPError struct { |
| 10 | + ErrorDetails HTTPErrorError `json:"error"` |
| 11 | + Meta HTTPErrorMeta `json:"meta"` |
| 12 | +} |
| 13 | + |
| 14 | +type HTTPErrorError struct { |
| 15 | + // Type is the type of the error. |
| 16 | + // Possible values: api_error, request_error. |
| 17 | + Type string `json:"type"` |
| 18 | + // Code is the error code. |
| 19 | + Code string `json:"code"` |
| 20 | + // Detail is the error message. |
| 21 | + Detail string `json:"detail"` |
| 22 | +} |
| 23 | + |
| 24 | +type HTTPErrorMeta struct { |
| 25 | + RequestID string `json:"request_id"` |
| 26 | +} |
| 27 | + |
| 28 | +func (h *HTTPError) Error() string { |
| 29 | + return fmt.Sprintf("error: %s, code: %s, detail: %s", h.ErrorDetails.Type, h.ErrorDetails.Code, h.ErrorDetails.Detail) |
| 30 | +} |
| 31 | + |
| 32 | +func (h *HTTPError) fullError() string { |
| 33 | + return fmt.Sprintf("error: %s, code: %s, detail: %s, request_id: %s", h.ErrorDetails.Type, h.ErrorDetails.Code, h.ErrorDetails.Detail, h.Meta.RequestID) |
| 34 | +} |
| 35 | + |
| 36 | +// Format implements fmt.Formatter interface. |
| 37 | +// It adds request ID to the error message when called with %+v verb. |
| 38 | +func (h *HTTPError) Format(s fmt.State, verb rune) { |
| 39 | + switch verb { |
| 40 | + case 'v': |
| 41 | + if s.Flag('+') { |
| 42 | + _, _ = io.WriteString(s, h.fullError()) |
| 43 | + return |
| 44 | + } |
| 45 | + fallthrough |
| 46 | + case 's': |
| 47 | + _, _ = io.WriteString(s, h.Error()) |
| 48 | + case 'q': |
| 49 | + _, _ = fmt.Fprintf(s, "%q", h.Error()) |
| 50 | + } |
| 51 | +} |
0 commit comments