功能修改
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"browser.local/platform/dto"
|
||||
"browser.local/platform/repo"
|
||||
@@ -60,11 +61,18 @@ func writeMethodNotAllowed(w http.ResponseWriter, allow string) {
|
||||
|
||||
func writeServiceError(w http.ResponseWriter, err error) {
|
||||
var validationErr validator.ValidationError
|
||||
var forbiddenErr service.ForbiddenError
|
||||
switch {
|
||||
case errors.As(err, &validationErr):
|
||||
writeAPIError(w, http.StatusBadRequest, errorCodeValidation, "validation failed", validationErr.Violations)
|
||||
case errors.Is(err, service.ErrUnauthorized):
|
||||
writeAPIError(w, http.StatusUnauthorized, errorCodeUnauthorized, "authentication required", nil)
|
||||
case errors.As(err, &forbiddenErr):
|
||||
message := strings.TrimSpace(forbiddenErr.Reason)
|
||||
if message == "" {
|
||||
message = "account is not allowed to access this resource"
|
||||
}
|
||||
writeAPIError(w, http.StatusForbidden, errorCodeForbidden, message, nil)
|
||||
case errors.Is(err, service.ErrForbidden):
|
||||
writeAPIError(w, http.StatusForbidden, errorCodeForbidden, "account is not allowed to access this resource", nil)
|
||||
case errors.Is(err, repo.ErrDuplicate):
|
||||
|
||||
Reference in New Issue
Block a user