47 lines
953 B
Go
47 lines
953 B
Go
package errors
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type AppError struct {
|
|
Code string
|
|
Message string
|
|
Err error
|
|
}
|
|
|
|
func (e *AppError) Error() string {
|
|
if e.Err != nil {
|
|
return fmt.Sprintf("%s: %s (code: %s)", e.Message, e.Err.Error(), e.Code)
|
|
}
|
|
return fmt.Sprintf("%s (code: %s)", e.Message, e.Code)
|
|
}
|
|
|
|
func (e *AppError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
func NewAppError(code, message string, err error) *AppError {
|
|
return &AppError{
|
|
Code: code,
|
|
Message: message,
|
|
Err: err,
|
|
}
|
|
}
|
|
|
|
func NewBadRequestError(message string, err error) *AppError {
|
|
return NewAppError("BAD_REQUEST", message, err)
|
|
}
|
|
|
|
func NewInternalError(message string, err error) *AppError {
|
|
return NewAppError("INTERNAL_ERROR", message, err)
|
|
}
|
|
|
|
func NewNotFoundError(message string, err error) *AppError {
|
|
return NewAppError("NOT_FOUND", message, err)
|
|
}
|
|
|
|
func NewUnauthorizedError(message string, err error) *AppError {
|
|
return NewAppError("UNAUTHORIZED", message, err)
|
|
}
|