feat(backend.domain): lab2 add new errors and new session repo
This commit is contained in:
@@ -14,14 +14,16 @@ const (
|
|||||||
CodeConflict ErrorCode = "conflict"
|
CodeConflict ErrorCode = "conflict"
|
||||||
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
||||||
CodeInternalError ErrorCode = "internal_error"
|
CodeInternalError ErrorCode = "internal_error"
|
||||||
|
CodeUnavailable ErrorCode = "unavailable"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNotFound = errors.New("не найдено")
|
ErrNotFound = errors.New("не найдено")
|
||||||
ErrAlreadyExists = errors.New("уже существует")
|
ErrAlreadyExists = errors.New("уже существует")
|
||||||
ErrInvalidInput = errors.New("некорректные данные")
|
ErrInvalidInput = errors.New("некорректные данные")
|
||||||
ErrForbidden = errors.New("доступ запрещён")
|
ErrUnauthorized = errors.New("требуется аутентификация")
|
||||||
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
||||||
|
ErrUnavailable = errors.New("сервис недоступен")
|
||||||
)
|
)
|
||||||
|
|
||||||
type NotFoundError struct {
|
type NotFoundError struct {
|
||||||
@@ -58,15 +60,15 @@ func (e *ValidationError) Error() string {
|
|||||||
func (e *ValidationError) Unwrap() error { return ErrInvalidInput }
|
func (e *ValidationError) Unwrap() error { return ErrInvalidInput }
|
||||||
func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest }
|
func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest }
|
||||||
|
|
||||||
type ForbiddenError struct {
|
type UnauthorizedError struct {
|
||||||
Reason string
|
Reason string // для логов; в JSON не попадает
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *ForbiddenError) Error() string {
|
func (e *UnauthorizedError) Error() string {
|
||||||
return fmt.Sprintf("доступ запрещён: %s", e.Reason)
|
return "требуется аутентификация"
|
||||||
}
|
}
|
||||||
func (e *ForbiddenError) Unwrap() error { return ErrForbidden }
|
func (e *UnauthorizedError) Unwrap() error { return ErrUnauthorized }
|
||||||
func (e *ForbiddenError) Code() ErrorCode { return CodeUnauthorized }
|
func (e *UnauthorizedError) Code() ErrorCode { return CodeUnauthorized }
|
||||||
|
|
||||||
type UnsupportedMediaTypeError struct {
|
type UnsupportedMediaTypeError struct {
|
||||||
MediaType string
|
MediaType string
|
||||||
@@ -80,3 +82,13 @@ func (e *UnsupportedMediaTypeError) Error() string {
|
|||||||
}
|
}
|
||||||
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
|
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
|
||||||
func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType }
|
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 }
|
||||||
|
|||||||
@@ -12,9 +12,16 @@ type ChatRepository interface {
|
|||||||
Create(ctx context.Context, chat *Chat) error
|
Create(ctx context.Context, chat *Chat) error
|
||||||
GetByID(ctx context.Context, id string) (*Chat, error)
|
GetByID(ctx context.Context, id string) (*Chat, error)
|
||||||
ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error)
|
ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error)
|
||||||
|
IsMember(ctx context.Context, chatID, userID string) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageRepository interface {
|
type MessageRepository interface {
|
||||||
Create(ctx context.Context, message *Message) error
|
Create(ctx context.Context, message *Message) error
|
||||||
ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error)
|
ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SessionRepository interface {
|
||||||
|
Create(ctx context.Context, session *Session) error
|
||||||
|
GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error)
|
||||||
|
DeleteByTokenHash(ctx context.Context, tokenHash []byte) error
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user