forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -4,6 +4,7 @@ package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/requestid"
|
||||
@@ -41,10 +42,13 @@ func NewError(code, message, requestID string) APIErrorResponse {
|
||||
}
|
||||
}
|
||||
|
||||
// AppError is the single contract error type. Handlers and the store return it;
|
||||
// the Fiber error handler is the only place that turns it into a response.
|
||||
type AppError struct {
|
||||
Status int
|
||||
Code string
|
||||
Message string
|
||||
Status int
|
||||
Code string
|
||||
Message string
|
||||
Challenge string // WWW-Authenticate value, set for 401 responses
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
@@ -60,31 +64,37 @@ func NewAppError(status int, code, message string) *AppError {
|
||||
}
|
||||
|
||||
func BadRequest(message string) *AppError {
|
||||
return NewAppError(fiber.StatusBadRequest, ErrorCodeInvalidRequest, message)
|
||||
}
|
||||
|
||||
func Unauthorized(message string) *AppError {
|
||||
return NewAppError(fiber.StatusUnauthorized, ErrorCodeUnauthorized, message)
|
||||
return NewAppError(http.StatusBadRequest, ErrorCodeInvalidRequest, message)
|
||||
}
|
||||
|
||||
func NotFound(message string) *AppError {
|
||||
return NewAppError(fiber.StatusNotFound, ErrorCodeNotFound, message)
|
||||
return NewAppError(http.StatusNotFound, ErrorCodeNotFound, message)
|
||||
}
|
||||
|
||||
func Conflict(message string) *AppError {
|
||||
return NewAppError(fiber.StatusConflict, ErrorCodeConflict, message)
|
||||
return NewAppError(http.StatusConflict, ErrorCodeConflict, message)
|
||||
}
|
||||
|
||||
func UnsupportedMedia(message string) *AppError {
|
||||
return NewAppError(fiber.StatusUnsupportedMediaType, ErrorCodeUnsupportedMedia, message)
|
||||
return NewAppError(http.StatusUnsupportedMediaType, ErrorCodeUnsupportedMedia, message)
|
||||
}
|
||||
|
||||
func Internal() *AppError {
|
||||
return NewAppError(fiber.StatusInternalServerError, ErrorCodeInternal, "Internal server error")
|
||||
return NewAppError(http.StatusInternalServerError, ErrorCodeInternal, "Internal server error")
|
||||
}
|
||||
|
||||
func Unavailable(message string) *AppError {
|
||||
return NewAppError(fiber.StatusServiceUnavailable, ErrorCodeUnavailable, message)
|
||||
return NewAppError(http.StatusServiceUnavailable, ErrorCodeUnavailable, message)
|
||||
}
|
||||
|
||||
// NewChallenge builds the 401 that carries a WWW-Authenticate header.
|
||||
func NewChallenge(challenge, message string) *AppError {
|
||||
return &AppError{
|
||||
Status: http.StatusUnauthorized,
|
||||
Code: ErrorCodeUnauthorized,
|
||||
Message: message,
|
||||
Challenge: challenge,
|
||||
}
|
||||
}
|
||||
|
||||
// IsCode reports whether err is an AppError with the given contract code.
|
||||
@@ -95,21 +105,21 @@ func IsCode(err error, code string) bool {
|
||||
|
||||
func ErrorCodeFromStatus(status int) string {
|
||||
switch status {
|
||||
case fiber.StatusBadRequest:
|
||||
case http.StatusBadRequest:
|
||||
return ErrorCodeInvalidRequest
|
||||
case fiber.StatusUnauthorized:
|
||||
case http.StatusUnauthorized:
|
||||
return ErrorCodeUnauthorized
|
||||
case fiber.StatusNotFound:
|
||||
case http.StatusNotFound:
|
||||
return ErrorCodeNotFound
|
||||
case fiber.StatusConflict:
|
||||
case http.StatusConflict:
|
||||
return ErrorCodeConflict
|
||||
case fiber.StatusRequestEntityTooLarge:
|
||||
case http.StatusRequestEntityTooLarge:
|
||||
return ErrorCodePayloadTooLarge
|
||||
case fiber.StatusUnsupportedMediaType:
|
||||
case http.StatusUnsupportedMediaType:
|
||||
return ErrorCodeUnsupportedMedia
|
||||
case fiber.StatusTooManyRequests:
|
||||
case http.StatusTooManyRequests:
|
||||
return ErrorCodeRateLimited
|
||||
case fiber.StatusServiceUnavailable:
|
||||
case http.StatusServiceUnavailable:
|
||||
return ErrorCodeUnavailable
|
||||
default:
|
||||
return ErrorCodeInternal
|
||||
@@ -126,17 +136,3 @@ func getRequestID(c fiber.Ctx) string {
|
||||
func SendError(c fiber.Ctx, status int, code, message string) error {
|
||||
return c.Status(status).JSON(NewError(code, message, getRequestID(c)))
|
||||
}
|
||||
|
||||
func SendAppError(c fiber.Ctx, err error) error {
|
||||
if appErr, ok := errors.AsType[*AppError](err); ok {
|
||||
return SendError(c, appErr.Status, appErr.Code, appErr.Message)
|
||||
}
|
||||
|
||||
return SendError(c, fiber.StatusInternalServerError,
|
||||
ErrorCodeInternal, "Internal server error")
|
||||
}
|
||||
|
||||
func SendChallenge(c fiber.Ctx, challenge, message string) error {
|
||||
c.Set(fiber.HeaderWWWAuthenticate, challenge)
|
||||
return SendError(c, fiber.StatusUnauthorized, ErrorCodeUnauthorized, message)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user