2023-12-20 07:03:03 +00:00
|
|
|
package domain
|
|
|
|
|
2024-03-01 06:46:42 +00:00
|
|
|
import "strconv"
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
type ErrorType uint8
|
2023-12-20 07:03:03 +00:00
|
|
|
|
|
|
|
const (
|
2024-02-01 06:48:03 +00:00
|
|
|
ErrorTypeUnknown ErrorType = iota
|
|
|
|
ErrorTypeNotFound
|
|
|
|
ErrorTypeIncorrectInput
|
2023-12-20 07:03:03 +00:00
|
|
|
)
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
func (e ErrorType) String() string {
|
2023-12-20 07:03:03 +00:00
|
|
|
switch e {
|
2024-02-01 06:48:03 +00:00
|
|
|
case ErrorTypeNotFound:
|
|
|
|
return "not found"
|
|
|
|
case ErrorTypeIncorrectInput:
|
|
|
|
return "incorrect input"
|
|
|
|
case ErrorTypeUnknown:
|
2023-12-20 07:03:03 +00:00
|
|
|
fallthrough
|
|
|
|
default:
|
2024-02-01 06:48:03 +00:00
|
|
|
return "unknown error"
|
2023-12-20 07:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Error interface {
|
|
|
|
error
|
2024-02-01 06:48:03 +00:00
|
|
|
Type() ErrorType
|
|
|
|
Code() string
|
2023-12-20 07:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorWithParams interface {
|
|
|
|
Error
|
|
|
|
Params() map[string]any
|
|
|
|
}
|
2023-12-27 08:07:40 +00:00
|
|
|
|
2024-03-01 06:46:42 +00:00
|
|
|
type ErrorPathSegment struct {
|
|
|
|
Model string
|
|
|
|
Field string
|
|
|
|
Index int // Index may be <0 and this means that it is unset
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ErrorPathSegment) String() string {
|
|
|
|
path := s.Model
|
|
|
|
|
|
|
|
if s.Field != "" {
|
|
|
|
if len(path) > 0 {
|
|
|
|
path += "."
|
|
|
|
}
|
|
|
|
path += s.Field
|
2024-03-11 09:05:29 +00:00
|
|
|
if s.Index >= 0 {
|
2024-03-01 06:46:42 +00:00
|
|
|
path += "[" + strconv.Itoa(s.Index) + "]"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorWithPath interface {
|
|
|
|
Error
|
|
|
|
Path() ErrorPathSegment
|
|
|
|
}
|
|
|
|
|
2023-12-27 08:07:40 +00:00
|
|
|
type simpleError struct {
|
|
|
|
msg string
|
2024-02-01 06:48:03 +00:00
|
|
|
typ ErrorType
|
|
|
|
code string
|
2023-12-27 08:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Error = simpleError{}
|
|
|
|
|
|
|
|
func (e simpleError) Error() string {
|
|
|
|
return e.msg
|
|
|
|
}
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
func (e simpleError) Type() ErrorType {
|
|
|
|
return e.typ
|
2023-12-27 08:07:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 06:48:03 +00:00
|
|
|
func (e simpleError) Code() string {
|
|
|
|
return e.code
|
2023-12-27 08:07:40 +00:00
|
|
|
}
|