forked from LFDT-Panurus/panurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
140 lines (121 loc) · 3.16 KB
/
Copy patherror.go
File metadata and controls
140 lines (121 loc) · 3.16 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package api
import (
"fmt"
"net/http"
)
// Error Enhanced Error interface giving extra-error information.
type Error interface {
Error() string
GetCode() int
GetMessage() string
GetLocation() string
GetCause() error
}
// AppError Implements Error
type AppError struct {
Code int `example:"400" json:"code"`
Message string `example:"Error message" json:"message"`
Cause error `json:"-"`
Location string `json:"-"`
}
func (e *AppError) Error() string {
if e.GetCause() == nil {
return fmt.Sprintf("[CODE %d] %s", e.GetCode(), e.GetMessage())
}
return fmt.Sprintf("[CODE %d] %s: %s", e.GetCode(), e.GetMessage(), e.GetCause().Error())
}
// GetCode Returns the error code.
func (e *AppError) GetCode() int {
return e.Code
}
// GetMessage Returns the error message.
func (e *AppError) GetMessage() string {
return e.Message
}
// GetLocation Returns the error location.
func (e *AppError) GetLocation() string {
return e.Location
}
// GetCause Returns the error.
func (e *AppError) GetCause() error {
return e.Cause
}
func (e *AppError) String() string {
return e.Error()
}
// NewBadRequestError Create a 400 Bad Request Error.
//
// err error The error to wrap.
// message string The generic message to show.
func NewBadRequestError(err error, message string) *AppError {
return &AppError{
Code: http.StatusBadRequest,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}
// NewNotFoundError Create a 404 Not Found Error.
//
// err error The error to wrap.
// message string The generic message to show.
func NewNotFoundError(err error, message string) *AppError {
return &AppError{
Code: http.StatusNotFound,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}
// NewInternalServerError Create a 500 Internal Server Error.
//
// err error The error to wrap.
// message string The generic message to show.
func NewInternalServerError(err error, message string) *AppError {
return &AppError{
Code: http.StatusInternalServerError,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}
// NewDBError Create a 500 Internal Server Error due to a DB failure.
//
// err error The error to wrap.
// message string The generic message to show.
func NewDBError(err error, message string) *AppError {
return &AppError{
Code: http.StatusInternalServerError,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}
// NewAuthorizationError Create a 401 Unauthorized.
//
// err error The error to wrap.
// message string The generic message to show.
func NewAuthorizationError(err error, message string) *AppError {
return &AppError{
Code: http.StatusUnauthorized,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}
// NewForbiddenError Create a 403 Forbidden Error.
//
// err error The error to wrap.
// message string The generic message to show.
func NewForbiddenError(err error, message string) *AppError {
return &AppError{
Code: http.StatusForbidden,
Message: message,
Cause: err,
// Location: utils.FileWithLineNum(),
}
}