package rest import ( "git.ali33.ru/fcg-xvii/go-tools/json" ) type IErrorArgs interface { Error() string Map() json.Map } ///////////////////////////////////////////////////// var ( ErrAlreadyOpened = "AlreadyOpened" ErrNotOpened = "NotOpened" ErrKeyNotExists = "KeyNotExists" ErrKeyInvalidType = "KeyInvalidType" ErrNotFound = "NotFound" ) func NewErrPrtexpected(field string) IErrorArgs { return &Error{ name: "PtrExpected", args: json.Map{ "field": field, }, } } func NewErrFieldRequired(field string) IErrorArgs { return &Error{ name: "FieldRequired", args: json.Map{ "field": field, }, } } func NewErrFieldType(field, expectedType, receivedType, message string, index int) IErrorArgs { args := json.Map{ "field": field, "expected": expectedType, "received": receivedType, } if len(message) > 0 { args["message"] = message } if index >= 0 { args["index"] = index } return &Error{ name: "FieldType", args: args, } } func NewError(name string, args json.Map) IErrorArgs { return &Error{ name: name, args: args, } } func NewErrorMessage(name, text string) IErrorArgs { return &Error{ name: name, args: json.Map{ "message": text, }, } } type Error struct { name string args json.Map } func (s *Error) Error() string { return s.Map().JSONPrettyString() } func (s *Error) Map() json.Map { return json.Map{ "name": s.name, "args": s.args, } }