-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
97 lines (78 loc) · 2.24 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sat
import (
"fmt"
"net/http"
"github.com/google/jsonapi"
)
const (
// SUCCESS_OK contains a success message
SUCCESS_OK = "OK"
// INVALID_SIGNATURE contains invalid signature message
INVALID_SIGNATURE = "INVALID_SIGNATURE"
// INVALID_PAYLOAD contains invalid payload message
INVALID_PAYLOAD = "INVALID_PAYLOAD"
// EMPTY_CLIENT_ID contains an empty client id error message
EMPTY_CLIENT_ID = "client id can't be empty"
// EMPTY_CLIENT_SECRET contains an empty client secret error message
EMPTY_CLIENT_SECRET = "client secret can't be empty"
// EMPTY_CLIENT_PRIVATE_KEY contains an empty client private key error message
EMPTY_CLIENT_PRIVATE_KEY = "client private key can't be empty"
)
// APIResponseError contains interface error method
type APIResponseError interface {
Error() string
Code() string
Status() string
Detail() string
}
// ErrorResponse wrapper api error
type ErrorResponse struct {
Errors []*ErrorObject `json:"errors"`
}
// ErrorObject is jsonapi.ErrorObject
type ErrorObject jsonapi.ErrorObject
// Error will convert all detail errors to one string
func (e *ErrorResponse) Error() string {
if len(e.Errors) <= 0 {
return ""
}
return fmt.Sprintf("%s - %s - %s\n", e.Errors[0].Status, e.Errors[0].Code, e.Errors[0].Detail)
}
// Code will parse code error and return it
func (e *ErrorResponse) Code() string {
if len(e.Errors) <= 0 {
return ""
}
return e.Errors[0].Code
}
// Status will parse status error and return it
func (e *ErrorResponse) Status() string {
if len(e.Errors) <= 0 {
return ""
}
return e.Errors[0].Status
}
// Detail will parse the detail error and return it
func (e *ErrorResponse) Detail() string {
if len(e.Errors) <= 0 {
return ""
}
return e.Errors[0].Detail
}
// APIInternalError for internal error produces by non-SAT server
type APIInternalError interface {
Error() string
Response() *http.Response
}
// InternalError wrapper internal error http response
type InternalError struct {
resp *http.Response
}
// Error will return http status code and http status as string
func (i *InternalError) Error() string {
return fmt.Sprintf("%d - %s\n", i.resp.StatusCode, i.resp.Status)
}
// Response will return http raw response
func (i *InternalError) Response() *http.Response {
return i.resp
}