2024-01-22 17:22:41: merge [master:56d3e06e49d7a75f142424293788a4f3b6245430];
This commit is contained in:
1 parent
7014f56cbb
commit
2b91caece0
60 files changed
+3558
No files matched your search
@@ -0,0 +1,41 @@
|
||||
package httperrors
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/integration-system/isp-kit/json"
|
||||
)
|
||||
|
||||
type HttpError struct {
|
||||
statusCode int
|
||||
userMessage string
|
||||
details []interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
func New(statusCode int, userMessage string, internalError error) HttpError {
|
||||
return HttpError{
|
||||
statusCode: statusCode,
|
||||
userMessage: userMessage,
|
||||
err: internalError,
|
||||
}
|
||||
}
|
||||
|
||||
func (e HttpError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
func (e HttpError) WriteError(w http.ResponseWriter) error {
|
||||
w.Header().Set("content-type", "application/json")
|
||||
w.WriteHeader(e.statusCode)
|
||||
data := map[string]interface{}{
|
||||
"errorCode": http.StatusText(e.statusCode),
|
||||
"errorMessage": e.userMessage,
|
||||
"details": e.details,
|
||||
}
|
||||
return json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func (e *HttpError) WithDetails(details ...interface{}) {
|
||||
e.details = details
|
||||
}
|
||||
Reference in new issue
Block a user