refactor(P2): Handler 错误码精确化 — service 层引入哨兵错误,handler 层用 ClassifyHTTPStatus 分类

This commit is contained in:
Sisyphus
2026-04-25 16:03:29 +08:00
parent 7d02262098
commit 025be6dd78
9 changed files with 1038 additions and 1001 deletions

View File

@@ -0,0 +1,28 @@
package http
import (
"errors"
"net/http"
"gorm.io/gorm"
)
var (
ErrUnauthorized = errors.New("unauthorized")
ErrValidation = errors.New("validation error")
)
func ClassifyHTTPStatus(err error) int {
switch {
case err == nil:
return http.StatusOK
case errors.Is(err, ErrUnauthorized):
return http.StatusUnauthorized
case errors.Is(err, ErrValidation):
return http.StatusBadRequest
case errors.Is(err, gorm.ErrRecordNotFound):
return http.StatusNotFound
default:
return http.StatusInternalServerError
}
}