-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
70 lines (58 loc) · 1.45 KB
/
Copy patherrors.go
File metadata and controls
70 lines (58 loc) · 1.45 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
// Package errors provides a custom error type for the application.
package errors
import (
"encoding/json"
"fmt"
)
type ICustomError interface {
Error() string
Log() string
serializeParams() (string, error)
}
type CustomError struct {
Module string `json:"-"`
Message string `json:"message"`
Params any `json:"error"`
Status int `json:"-"`
}
func (e *CustomError) serializeParams() (string, error) {
byt, err := json.Marshal(e.Params)
return string(byt), err
}
func (e *CustomError) serialize() (string, error) {
byt, err := json.Marshal(e)
return string(byt), err
}
func (e CustomError) Error() string {
msg, err := e.serialize()
if err != nil {
return "Serialize error failed"
}
return msg
}
func (e *CustomError) Log() string {
params, err := e.serializeParams()
if err != nil {
return "Serialize params failed"
}
return fmt.Sprintf("[%s] # [%s] : [%s]", e.Module, e.Message, params)
}
// GetStatus returns the HTTP status code for Huma (StatusError).
func (e *CustomError) GetStatus() int { return e.Status }
func New(module string, message string) *CustomError {
return &CustomError{
Module: module,
Message: message,
Params: nil,
Status: 500,
}
}
// NewWithStatus returns a CustomError with the given status (e.g. for Huma default errors).
func NewWithStatus(module string, message string, status int) *CustomError {
return &CustomError{
Module: module,
Message: message,
Params: nil,
Status: status,
}
}