This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
86 lines (79 loc) · 2.12 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
package jexiasdkgo
import (
"fmt"
"io/ioutil"
"net/http"
)
// Internal signifies the error was send from an internal source
const Internal = "Internal"
// API signifies the error was send from the Jexia API directly
const API = "API"
// Error is the error returned, allows for retry scenarios
type Error struct {
ID string `json:"request_id"`
Message string `json:"message"`
Origin string `json:"origin"`
Temporary bool `json:"temporary"`
}
func (e *Error) Error() string {
temp := ""
if e.Temporary {
temp = "temporary"
}
return fmt.Sprintf("%v endpoint error: %v (Origin: %v) (ID: %v)", temp, e.Message, e.Origin, e.ID)
}
// checkForAPIError is an internal function wrapper for returning a more useful API error
func checkForAPIError(response *http.Response) error {
// Success is indicated with 2xx status codes:
if response.StatusCode >= 200 && response.StatusCode < 300 {
return nil
}
var APIErr []Error
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return &Error{
ID: "e001",
Message: fmt.Errorf("Unable to read response body: %w", err).Error(),
Origin: Internal,
Temporary: false,
}
}
err = unmarshal(b, &APIErr)
if err != nil {
return &Error{
ID: "e002",
Message: fmt.Errorf("Unable to unmarshal body: %w", err).Error(),
Origin: Internal,
Temporary: false,
}
}
// If there is a matching API error, return this as we can fail absolutely
if len(APIErr) > 0 {
return &Error{
ID: APIErr[0].ID,
Message: APIErr[0].Message,
Origin: API,
Temporary: false,
}
}
// Such unknown error may allow us to retry, it could be due to a network connection drop etc.
return &Error{
ID: "e003",
Message: fmt.Errorf("Unknown error, error does not match predefined parameters, presumed failed call").Error(),
Origin: Internal,
Temporary: true,
}
}
func getNiceError(err error, message string) *Error {
switch err.(type) {
case *Error:
return err.(*Error)
default:
return &Error{
ID: "e004",
Message: fmt.Errorf("%v: %v", message, err).Error(),
Origin: Internal,
Temporary: false,
}
}
}