feat(app) lab2 prepared for mr

This commit is contained in:
imadev
2026-10-23 18:55:20 +03:00
parent 1db3953226
commit f6e74247fd
57 changed files with 2367 additions and 660 deletions
+22 -1
View File
@@ -4,6 +4,7 @@ import (
"errors"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/requestid"
)
const (
@@ -80,6 +81,16 @@ func Internal() *AppError {
return NewAppError(fiber.StatusInternalServerError, ErrorCodeInternal, "Internal server error")
}
func Unavailable(message string) *AppError {
return NewAppError(fiber.StatusServiceUnavailable, ErrorCodeUnavailable, message)
}
// 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
}
func ErrorCodeFromStatus(status int) string {
switch status {
case fiber.StatusBadRequest:
@@ -104,7 +115,10 @@ func ErrorCodeFromStatus(status int) string {
}
func getRequestID(c fiber.Ctx) string {
return string(c.Response().Header.Peek("X-Request-Id"))
if id := string(c.Response().Header.Peek("X-Request-Id")); id != "" {
return id
}
return requestid.FromContext(c)
}
func SendError(c fiber.Ctx, status int, code, message string) error {
@@ -120,3 +134,10 @@ func SendAppError(c fiber.Ctx, err error) error {
return SendError(c, fiber.StatusInternalServerError,
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)
}