mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
fix: add http error responses (#494)
This features adds rfc7807 Problem detail responses when an error happens processing a request. This will greatly improve the common issues with "blank pages" and "404 pages" issues which should now properly tell the user what input was wrong (group that does not exist, container name that does not exist, etc.)
This commit is contained in:
47
internal/server/logging.go
Normal file
47
internal/server/logging.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
sloggin "github.com/samber/slog-gin"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// StructuredLogger logs a gin HTTP request in JSON format. Allows to set the
|
||||
// logger for testing purposes.
|
||||
func StructuredLogger(logger *slog.Logger) gin.HandlerFunc {
|
||||
if logger.Enabled(nil, slog.LevelDebug) {
|
||||
return sloggin.NewWithConfig(logger, sloggin.Config{
|
||||
DefaultLevel: slog.LevelInfo,
|
||||
ClientErrorLevel: slog.LevelWarn,
|
||||
ServerErrorLevel: slog.LevelError,
|
||||
|
||||
WithUserAgent: false,
|
||||
WithRequestID: true,
|
||||
WithRequestBody: false,
|
||||
WithRequestHeader: false,
|
||||
WithResponseBody: false,
|
||||
WithResponseHeader: false,
|
||||
WithSpanID: false,
|
||||
WithTraceID: false,
|
||||
|
||||
Filters: []sloggin.Filter{},
|
||||
})
|
||||
}
|
||||
|
||||
return sloggin.NewWithConfig(logger, sloggin.Config{
|
||||
DefaultLevel: slog.LevelInfo,
|
||||
ClientErrorLevel: slog.LevelWarn,
|
||||
ServerErrorLevel: slog.LevelError,
|
||||
|
||||
WithUserAgent: false,
|
||||
WithRequestID: true,
|
||||
WithRequestBody: false,
|
||||
WithRequestHeader: false,
|
||||
WithResponseBody: false,
|
||||
WithResponseHeader: false,
|
||||
WithSpanID: false,
|
||||
WithTraceID: false,
|
||||
|
||||
Filters: []sloggin.Filter{},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user