-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
84 lines (77 loc) · 2.86 KB
/
errors.go
File metadata and controls
84 lines (77 loc) · 2.86 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
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
package xmatters
import (
"encoding/json"
"fmt"
"path/filepath"
"runtime"
"strings"
)
var (
// ErrNoContent is a generic 204 Error output used to return appropriate output to the user after a failed DELETE request.
ErrNoContent = XMattersError{
Code: StatusNoContent,
Message: "A resource was not found in response to a DELETE request.",
Reason: "No Content",
}
// ErrInavlidCredentials is a generic 401 Error output used to return appropriate output to the user after a failed request due to invalid credentials.
ErrInavlidCredentials = XMattersError{
Code: StatusUnauthorized,
Message: "Invalid Credentials",
Reason: "Unauthorized",
}
// ErrNoHostname is a generic Error output used to return appropriate output to the user after a failed request due to missing hostname.
ErrNoHostname = XMattersError{
Code: 0,
Message: "Missing Hostname",
Reason: "Bad Request",
}
// General error message content
errUnmarshalError = "error unmarshalling the JSON response"
errUnmarshalErrorBody = "error unmarshalling the JSON response error body"
)
// XMattersError is a custom error type with helpful fields.
type XMattersError struct {
Code int `json:"code,omitempty"`
Reason string `json:"reason"`
Message string `json:"message"`
Subcode string `json:"subcode,omitempty"`
}
// Error implements the error interface for xMattersError.
func (e XMattersError) Error() string {
return fmt.Sprintf("xMatters API Error: %d - %s. %s\nSubcode: %s", e.Code, e.Reason, e.Message, e.Subcode)
}
// getFunctionName retrieves the name of the function that called `newUnmarshalError`.
// It uses runtime.Caller to get the program counter and function name.
func getFunctionName() string {
pc, _, _, ok := runtime.Caller(2) // 2 means two levels up from this function (the function that called `newUnmarshalError`)
if !ok {
return "unknown"
}
fn := runtime.FuncForPC(pc)
if fn == nil {
return "unknown"
}
// Extract function name without package
fullName := fn.Name()
return filepath.Base(fullName[strings.LastIndex(fullName, ".")+1:])
}
// newUnmarshalError creates a new XMattersError with a generic unmarshal error message.
// It uses the getFunctionName function to include the name of the function that called it.
// This is useful for debugging and understanding where the error occurred.
// The error code is set to 0, indicating a generic error.
func newUnmarshalError() error {
return XMattersError{
Code: 0,
Message: errUnmarshalError,
Reason: fmt.Sprintf("Internal Server Error in %s", getFunctionName()),
}
}
// NewXMattersError is a constructor function to create a new xMattersError instance
func newXMattersError(body []byte) error {
var xmerr XMattersError
err := json.Unmarshal(body, &xmerr)
if err != nil {
return fmt.Errorf("%s in xMatters Error Construction: %w \n%s", errUnmarshalErrorBody, err, string(body))
}
return xmerr
}