-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
39 lines (32 loc) · 1.15 KB
/
model.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
package errors
import (
"encoding/json"
"encoding/xml"
)
type Validation struct {
Tag string `json:"tag,omitempty" xml:"tag,omitempty" yaml:"tag,omitempty"`
Field string `json:"field,omitempty" xml:"field,omitempty" yaml:"field,omitempty"`
}
type Model struct {
Code uint32 `json:"code,omitempty" xml:"code,omitempty" yaml:"code,omitempty"`
Message string `json:"message,omitempty" xml:"message,omitempty" yaml:"message,omitempty"`
Details []string `json:"details,omitempty" xml:"details,omitempty" yaml:"details,omitempty"`
Validations []*Validation `json:"validations,omitempty" xml:"validations,omitempty" yaml:"validations,omitempty"`
}
func (e Error) Model() *Model {
return &Model{
Code: uint32(e.Code()),
Message: e.Localize(),
Details: append([]string{e.Message()}, e.Details()...),
Validations: e.Validations(),
}
}
func (e Error) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Model())
}
func (e Error) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
return encoder.EncodeElement(e.Model(), start)
}
func (e Error) MarshalYAML() (any, error) {
return e.Model(), nil
}