feat: initial commit

This commit is contained in:
2025-06-10 21:09:58 +02:00
commit 1fb753469e
84 changed files with 3912 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package common
import (
"log/slog"
"net/http"
"forge.cadoles.com/wpetit/kouiz/internal/http/handler/webui/common/component"
"github.com/a-h/templ"
"github.com/pkg/errors"
)
type HTTPError interface {
error
StatusCode() int
}
type UserFacingError interface {
error
UserMessage() string
}
func HandleError(w http.ResponseWriter, r *http.Request, err error) {
vmodel := component.ErrorPageVModel{}
statusCode := http.StatusInternalServerError
var httpErr HTTPError
if errors.As(err, &httpErr) {
statusCode = httpErr.StatusCode()
}
w.WriteHeader(statusCode)
var userFacingErr UserFacingError
if errors.As(err, &userFacingErr) {
vmodel.Message = userFacingErr.UserMessage()
} else {
vmodel.Message = http.StatusText(statusCode)
}
if httpErr == nil && userFacingErr == nil {
slog.ErrorContext(r.Context(), "unexpected error", slog.Any("error", errors.WithStack(err)))
}
errorPage := component.ErrorPage(vmodel)
templ.Handler(errorPage).ServeHTTP(w, r)
}