feat: port backend implementation from lab1
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
CodeInvalidRequest ErrorCode = "invalid_request"
|
||||
CodeUnauthorized ErrorCode = "unauthorized"
|
||||
CodeNotFound ErrorCode = "not_found"
|
||||
CodeConflict ErrorCode = "conflict"
|
||||
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
||||
CodeInternalError ErrorCode = "internal_error"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("не найдено")
|
||||
ErrAlreadyExists = errors.New("уже существует")
|
||||
ErrInvalidInput = errors.New("некорректные данные")
|
||||
ErrForbidden = errors.New("доступ запрещён")
|
||||
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
||||
)
|
||||
|
||||
type NotFoundError struct {
|
||||
Entity string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (e *NotFoundError) Error() string {
|
||||
return fmt.Sprintf("%s с id %q не найден", e.Entity, e.ID)
|
||||
}
|
||||
func (e *NotFoundError) Unwrap() error { return ErrNotFound }
|
||||
func (e *NotFoundError) Code() ErrorCode { return CodeNotFound }
|
||||
|
||||
type AlreadyExistsError struct {
|
||||
Entity string
|
||||
Field string
|
||||
Value string
|
||||
}
|
||||
|
||||
func (e *AlreadyExistsError) Error() string {
|
||||
return fmt.Sprintf("%s с %s %q уже существует", e.Entity, e.Field, e.Value)
|
||||
}
|
||||
func (e *AlreadyExistsError) Unwrap() error { return ErrAlreadyExists }
|
||||
func (e *AlreadyExistsError) Code() ErrorCode { return CodeConflict }
|
||||
|
||||
type ValidationError struct {
|
||||
Field string
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ValidationError) Error() string {
|
||||
return fmt.Sprintf("ошибка валидации поля %q: %s", e.Field, e.Message)
|
||||
}
|
||||
func (e *ValidationError) Unwrap() error { return ErrInvalidInput }
|
||||
func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest }
|
||||
|
||||
type ForbiddenError struct {
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (e *ForbiddenError) Error() string {
|
||||
return fmt.Sprintf("доступ запрещён: %s", e.Reason)
|
||||
}
|
||||
func (e *ForbiddenError) Unwrap() error { return ErrForbidden }
|
||||
func (e *ForbiddenError) Code() ErrorCode { return CodeUnauthorized }
|
||||
|
||||
type UnsupportedMediaTypeError struct {
|
||||
MediaType string
|
||||
}
|
||||
|
||||
func (e *UnsupportedMediaTypeError) Error() string {
|
||||
if e.MediaType == "" {
|
||||
return "требуется Content-Type: application/json"
|
||||
}
|
||||
return fmt.Sprintf("неподдерживаемый Content-Type %q; требуется application/json", e.MediaType)
|
||||
}
|
||||
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
|
||||
func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType }
|
||||
Reference in New Issue
Block a user