The spec is now built from the same calls that register the routes, so it
can't drift from the router like the swag comments did (all /api/v1
routes were missing).
- /api/v1 endpoints are typed Huma operations; the spec is OpenAPI 3.1 at
/openapi.json (3.0 at /openapi-3.0.json, YAML too), docs at /docs.
/openapi and /swagger/* redirect there
- Bearer token security is declared per operation; token creation and
the new GET /api/v1/health are public
- Legacy routes use the same handlers, are hidden from the spec and keep
the Deprecation header
- /sync/reset is hidden (debug only); the /sync/new, /sync/quick and
/sync/new/full aliases are removed (use /sync and /sync/full)
- Invalid parameters return 422 (validation); every error is
{"error": "..."}; empty lists are [] instead of null
- /sync/progress is documented as progress or result (oneOf)
- Tests validate the spec and check real responses of every documented
operation against it (kin-openapi)
- Removed swag annotations, cmd/docs, echo-swagger and the swag tool
43 lines
991 B
Go
43 lines
991 B
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"music-server/internal/logging"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// httpErrorHandler writes errors from Echo (unknown routes, panics, ...) as an APIError, like Huma does.
|
|
func httpErrorHandler(c *echo.Context, err error) {
|
|
if r, _ := echo.UnwrapResponse(c.Response()); r != nil && r.Committed {
|
|
return
|
|
}
|
|
|
|
code := http.StatusInternalServerError
|
|
message := http.StatusText(code)
|
|
var he *echo.HTTPError
|
|
var sc echo.HTTPStatusCoder
|
|
switch {
|
|
case errors.As(err, &he):
|
|
code = he.Code
|
|
message = he.Message
|
|
case errors.As(err, &sc):
|
|
code = sc.StatusCode()
|
|
}
|
|
if code >= http.StatusInternalServerError && he == nil {
|
|
logging.GetLogger().Error("Unhandled error", zap.String("path", c.Request().URL.Path), zap.Error(err))
|
|
}
|
|
if message == "" {
|
|
message = http.StatusText(code)
|
|
}
|
|
|
|
if c.Request().Method == http.MethodHead {
|
|
_ = c.NoContent(code)
|
|
return
|
|
}
|
|
_ = c.JSON(code, &APIError{Message: message})
|
|
}
|