refactor(backend): switch to DDD
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user