Files
mtusi2026lab2_fork/backend/internal/domain/errors.go
T

95 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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"
CodeUnavailable ErrorCode = "unavailable"
)
var (
ErrNotFound = errors.New("не найдено")
ErrAlreadyExists = errors.New("уже существует")
ErrInvalidInput = errors.New("некорректные данные")
ErrUnauthorized = errors.New("требуется аутентификация")
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
ErrUnavailable = 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 UnauthorizedError struct {
Reason string // для логов; в JSON не попадает
}
func (e *UnauthorizedError) Error() string {
return "требуется аутентификация"
}
func (e *UnauthorizedError) Unwrap() error { return ErrUnauthorized }
func (e *UnauthorizedError) 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 }
type UnavailableError struct {
Reason string // для логов; в JSON не попадает
}
func (e *UnavailableError) Error() string {
return "сервис временно недоступен"
}
func (e *UnavailableError) Unwrap() error { return ErrUnavailable }
func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable }