style(backend): improve error handling

This commit is contained in:
2025-10-10 01:37:01 +03:00
parent d124229308
commit b774d2b3c9
4 changed files with 115 additions and 85 deletions
@@ -1,7 +1,6 @@
package rest
import (
"fmt"
"net/http"
"tanabata/internal/domain"
@@ -17,28 +16,28 @@ type ErrorMapper struct{}
func (m *ErrorMapper) MapError(err domain.DomainError) (int, ErrorResponse) {
switch err.Code {
case domain.ErrFileNotFound:
case domain.ErrCodeFileNotFound:
return http.StatusNotFound, ErrorResponse{
Error: "Not Found",
Code: string(err.Code),
Message: fmt.Sprintf("File %q not found", err.Details...),
Message: err.Message,
}
case domain.ErrMIMENotSupported:
case domain.ErrCodeMIMENotSupported:
return http.StatusNotFound, ErrorResponse{
Error: "MIME not supported",
Code: string(err.Code),
Message: fmt.Sprintf("MIME not supported: %q", err.Details...),
Message: err.Message,
}
case domain.ErrValidation:
case domain.ErrCodeBadRequest:
return http.StatusNotFound, ErrorResponse{
Error: "Bad Request",
Code: string(err.Code),
Message: fmt.Sprintf("Invalid %s: %s", err.Details...),
Message: err.Message,
}
}
return http.StatusInternalServerError, ErrorResponse{
Error: "Internal Server Error",
Code: string(err.Code),
Message: "An unexpected error occured",
Message: err.Message,
}
}