feat: user domain -- errors

This commit is contained in:
2025-09-02 16:38:12 +03:30
parent 5ea90d5c0e
commit 0e32d0d820
11 changed files with 195 additions and 1 deletions
View File
+6
View File
@@ -0,0 +1,6 @@
package errors
var (
ErrPhoneInvalid = NewAppError(400, "Invalid phone number", ErrorTypeValidation, nil)
ErrNationalIDInvalid = NewAppError(400, "Invalid national ID", ErrorTypeValidation, nil)
)
+51
View File
@@ -0,0 +1,51 @@
package errors
import (
"errors"
)
type ErrorType string
const (
ErrorTypeValidation ErrorType = "VALIDATION_ERROR"
ErrorTypeAuth ErrorType = "AUTH_ERROR"
)
type AppError struct {
Code int `json:"code"`
Message string `json:"message"`
Type ErrorType `json:"type"`
Cause error `json:"_"`
}
func NewAppError(code int, msg string, typ ErrorType, cause error) *AppError {
return &AppError{
Code: code,
Message: msg,
Type: typ,
Cause: cause,
}
}
func (e *AppError) Error() string {
return e.Message
}
func (e *AppError) Unwrap() error {
return e.Cause
}
func (e *AppError) Is(target error) bool {
if t, ok := target.(*AppError); ok {
return e.Type == t.Type
}
return errors.Is(e.Cause, target)
}
func (e *AppError) As(target interface{}) bool {
if t, ok := target.(**AppError); ok {
*t = e
return true
}
return errors.As(e.Cause, target)
}