66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package domain
|
|
|
|
import "fmt"
|
|
|
|
type ErrorCode string
|
|
|
|
const (
|
|
// File errors
|
|
ErrCodeFileNotFound ErrorCode = "FILE_NOT_FOUND"
|
|
ErrCodeMIMENotSupported ErrorCode = "MIME_NOT_SUPPORTED"
|
|
|
|
// Tag errors
|
|
ErrCodeTagNotFound ErrorCode = "TAG_NOT_FOUND"
|
|
|
|
// General errors
|
|
ErrCodeBadRequest ErrorCode = "BAD_REQUEST"
|
|
ErrCodeInternal ErrorCode = "INTERNAL_SERVER_ERROR"
|
|
)
|
|
|
|
type DomainError struct {
|
|
Err error `json:"-"`
|
|
Code ErrorCode `json:"code"`
|
|
Message string `json:"message"`
|
|
Details []any `json:"-"`
|
|
}
|
|
|
|
func (e *DomainError) Wrap(err error) *DomainError {
|
|
e.Err = err
|
|
return e
|
|
}
|
|
|
|
func NewErrorFileNotFound(file_id string) *DomainError {
|
|
return &DomainError{
|
|
Code: ErrCodeFileNotFound,
|
|
Message: fmt.Sprintf("File not found: %q", file_id),
|
|
}
|
|
}
|
|
|
|
func NewErrorMIMENotSupported(mime string) *DomainError {
|
|
return &DomainError{
|
|
Code: ErrCodeMIMENotSupported,
|
|
Message: fmt.Sprintf("MIME not supported: %q", mime),
|
|
}
|
|
}
|
|
|
|
func NewErrorTagNotFound(tag_id string) *DomainError {
|
|
return &DomainError{
|
|
Code: ErrCodeTagNotFound,
|
|
Message: fmt.Sprintf("Tag not found: %q", tag_id),
|
|
}
|
|
}
|
|
|
|
func NewErrorBadRequest(message string) *DomainError {
|
|
return &DomainError{
|
|
Code: ErrCodeBadRequest,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
func NewErrorUnexpected() *DomainError {
|
|
return &DomainError{
|
|
Code: ErrCodeInternal,
|
|
Message: "An unexpected error occured",
|
|
}
|
|
}
|