-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
59 lines (48 loc) · 1.51 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
package termiigo
import (
"encoding/json"
)
// / APIResponse is a custom interface representing the necessary parts of an HTTP response
type APIResponse interface {
GetStatusCode() int
GetBody() []byte
}
// HTTPResponse implements the APIResponse interface
type HTTPResponse struct {
StatusCode int
Body []byte
}
func (r *HTTPResponse) GetStatusCode() int {
return r.StatusCode
}
func (r *HTTPResponse) GetBody() []byte {
return r.Body
}
// APIError includes the response from the Termii API and some HTTP request info
type APIError struct {
HTTPStatusCode int `json:"code,omitempty"`
Details ErrorResponse `json:"details,omitempty"`
}
// APIError supports the error interface
func (apiErr *APIError) Error() string {
ret, _ := json.Marshal(apiErr)
return string(ret)
}
// ErrorResponse represents an error response from the Paystack API server
type ErrorResponse struct {
Message string `json:"message,omitempty"`
Errors interface{} `json:"errors,omitempty"`
}
func newAPIError(resp APIResponse) *APIError {
var termiiErrorResponse ErrorResponse
if resp != nil {
// Assuming you have a way to parse the response body into termiiErrorResponse
// For example, if the response body is JSON, you can use json.Unmarshal(resp.GetBody(), &termiiErrorResponse)
// Ensure you handle any errors during decoding
// For simplicity, let's assume termiiErrorResponse is populated correctly
}
return &APIError{
HTTPStatusCode: resp.GetStatusCode(),
Details: termiiErrorResponse,
}
}