45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package rest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"tanabata/internal/domain"
|
|
)
|
|
|
|
type ErrorResponse struct {
|
|
Error string `json:"error"`
|
|
Code string `json:"code,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
type ErrorMapper struct{}
|
|
|
|
func (m *ErrorMapper) MapError(err domain.DomainError) (int, ErrorResponse) {
|
|
switch err.Code {
|
|
case domain.ErrFileNotFound:
|
|
return http.StatusNotFound, ErrorResponse{
|
|
Error: "Not Found",
|
|
Code: string(err.Code),
|
|
Message: fmt.Sprintf("File %q not found", err.Details...),
|
|
}
|
|
case domain.ErrMIMENotSupported:
|
|
return http.StatusNotFound, ErrorResponse{
|
|
Error: "MIME not supported",
|
|
Code: string(err.Code),
|
|
Message: fmt.Sprintf("MIME not supported: %q", err.Details...),
|
|
}
|
|
case domain.ErrValidation:
|
|
return http.StatusNotFound, ErrorResponse{
|
|
Error: "Bad Request",
|
|
Code: string(err.Code),
|
|
Message: fmt.Sprintf("Invalid %s: %s", err.Details...),
|
|
}
|
|
}
|
|
return http.StatusInternalServerError, ErrorResponse{
|
|
Error: "Internal Server Error",
|
|
Code: string(err.Code),
|
|
Message: "An unexpected error occured",
|
|
}
|
|
}
|