forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// Package common contains cross-cutting HTTP helpers: contract errors, JSON
|
||||
// binding, validation, cursor pagination and timestamp formatting.
|
||||
package common
|
||||
|
||||
import (
|
||||
@@ -87,8 +89,8 @@ func Unavailable(message string) *AppError {
|
||||
|
||||
// IsCode reports whether err is an AppError with the given contract code.
|
||||
func IsCode(err error, code string) bool {
|
||||
var appErr *AppError
|
||||
return errors.As(err, &appErr) && appErr.Code == code
|
||||
appErr, ok := errors.AsType[*AppError](err)
|
||||
return ok && appErr.Code == code
|
||||
}
|
||||
|
||||
func ErrorCodeFromStatus(status int) string {
|
||||
@@ -126,8 +128,7 @@ func SendError(c fiber.Ctx, status int, code, message string) error {
|
||||
}
|
||||
|
||||
func SendAppError(c fiber.Ctx, err error) error {
|
||||
var appErr *AppError
|
||||
if errors.As(err, &appErr) {
|
||||
if appErr, ok := errors.AsType[*AppError](err); ok {
|
||||
return SendError(c, appErr.Status, appErr.Code, appErr.Message)
|
||||
}
|
||||
|
||||
@@ -135,8 +136,6 @@ func SendAppError(c fiber.Ctx, err error) error {
|
||||
ErrorCodeInternal, "Internal server error")
|
||||
}
|
||||
|
||||
// SendChallenge emits a 401 with the WWW-Authenticate value required by the
|
||||
// contract: Basic for login, Bearer for every protected endpoint.
|
||||
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