feat(domain):lab4 add attachments, status, errors
This commit is contained in:
@@ -22,11 +22,12 @@ type Chat struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
ID string
|
ID string
|
||||||
ChatID string
|
ChatID string
|
||||||
SenderID string
|
SenderID string
|
||||||
Text string
|
Text string
|
||||||
CreatedAt time.Time
|
AttachmentIDs []string
|
||||||
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
@@ -35,3 +36,26 @@ type Session struct {
|
|||||||
ExpiresAt time.Time
|
ExpiresAt time.Time
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AttachmentStatus string
|
||||||
|
|
||||||
|
const (
|
||||||
|
AttachmentStatusPending AttachmentStatus = "pending"
|
||||||
|
AttachmentStatusReady AttachmentStatus = "ready"
|
||||||
|
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Attachment struct {
|
||||||
|
ID string
|
||||||
|
ChatID string
|
||||||
|
OwnerID string
|
||||||
|
Filename string
|
||||||
|
ContentType string
|
||||||
|
SizeBytes int64
|
||||||
|
SHA256 []byte
|
||||||
|
ObjectKey string
|
||||||
|
Status AttachmentStatus
|
||||||
|
ErrorCode *string
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const (
|
|||||||
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
||||||
CodeInternalError ErrorCode = "internal_error"
|
CodeInternalError ErrorCode = "internal_error"
|
||||||
CodeUnavailable ErrorCode = "unavailable"
|
CodeUnavailable ErrorCode = "unavailable"
|
||||||
|
CodePayloadTooLarge ErrorCode = "payload_too_large"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -24,6 +25,7 @@ var (
|
|||||||
ErrUnauthorized = errors.New("требуется аутентификация")
|
ErrUnauthorized = errors.New("требуется аутентификация")
|
||||||
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
||||||
ErrUnavailable = errors.New("сервис недоступен")
|
ErrUnavailable = errors.New("сервис недоступен")
|
||||||
|
ErrPayloadTooLarge = errors.New("превышен допустимый размер")
|
||||||
)
|
)
|
||||||
|
|
||||||
type NotFoundError struct {
|
type NotFoundError struct {
|
||||||
@@ -94,12 +96,27 @@ 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 {
|
type UnavailableError struct{ Reason string }
|
||||||
Reason string // для логов; в JSON не попадает
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *UnavailableError) Error() string {
|
func (e *UnavailableError) Error() string {
|
||||||
return "сервис временно недоступен"
|
return "сервис временно недоступен"
|
||||||
}
|
}
|
||||||
func (e *UnavailableError) Unwrap() error { return ErrUnavailable }
|
func (e *UnavailableError) Unwrap() error { return ErrUnavailable }
|
||||||
func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable }
|
func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable }
|
||||||
|
|
||||||
|
// PayloadTooLargeError - 413. Лимит кладём в структуру, чтобы его можно
|
||||||
|
// было переиспользовать в тексте ошибки, если Message не задан.
|
||||||
|
// Message опционален: если пуст — формируется из Limit.
|
||||||
|
type PayloadTooLargeError struct {
|
||||||
|
Limit int64
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *PayloadTooLargeError) Error() string {
|
||||||
|
if e.Message != "" {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("превышен лимит %d байт", e.Limit)
|
||||||
|
}
|
||||||
|
func (e *PayloadTooLargeError) Unwrap() error { return ErrPayloadTooLarge }
|
||||||
|
func (e *PayloadTooLargeError) Code() ErrorCode { return CodePayloadTooLarge }
|
||||||
|
|||||||
@@ -35,3 +35,13 @@ type SessionRepository interface {
|
|||||||
GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error)
|
GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error)
|
||||||
DeleteByTokenHash(ctx context.Context, tokenHash []byte) error
|
DeleteByTokenHash(ctx context.Context, tokenHash []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AttachmentRepository interface {
|
||||||
|
Create(ctx context.Context, a *Attachment) error
|
||||||
|
GetByID(ctx context.Context, id string) (*Attachment, error)
|
||||||
|
MarkReady(ctx context.Context, id string) error
|
||||||
|
MarkFailed(ctx context.Context, id string, errorCode string) error
|
||||||
|
ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error)
|
||||||
|
ExistingKeys(ctx context.Context, keys []string) ([]string, error)
|
||||||
|
DeletePending(ctx context.Context, id string) error
|
||||||
|
}
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ func StatusFromError(err error) int {
|
|||||||
return 415
|
return 415
|
||||||
case errors.Is(err, domain.ErrUnavailable):
|
case errors.Is(err, domain.ErrUnavailable):
|
||||||
return 503
|
return 503
|
||||||
|
case errors.Is(err, domain.ErrPayloadTooLarge):
|
||||||
|
return 413
|
||||||
default:
|
default:
|
||||||
return 500
|
return 500
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user