Compare commits

...

4 Commits

Author SHA1 Message Date
7ed40c7afe added error wrapping to auth middleware for better stacktrace 2026-01-26 21:56:47 +11:00
596a4c0529 fixed stacktrace 2026-01-26 21:48:18 +11:00
ed3bc4afb0 added stacktrace to error logging 2026-01-26 21:45:53 +11:00
2c9de70018 fixed bug in NewMiddleware 2026-01-26 21:38:02 +11:00
3 changed files with 21 additions and 9 deletions

View File

@@ -20,25 +20,25 @@ func (s *Server) LogError(err HWSError) {
}
switch err.Level {
case ErrorDEBUG:
s.logger.logger.Debug().Err(err.Error).Msg(err.Message)
s.logger.logger.Debug().Msg(err.Message)
return
case ErrorINFO:
s.logger.logger.Info().Err(err.Error).Msg(err.Message)
s.logger.logger.Info().Msg(err.Message)
return
case ErrorWARN:
s.logger.logger.Warn().Err(err.Error).Msg(err.Message)
return
case ErrorERROR:
s.logger.logger.Error().Err(err.Error).Msg(err.Message)
s.logger.logger.Error().Str("stacktrace", fmt.Sprintf("%+v", err.Error)).Err(err.Error).Msg(err.Message)
return
case ErrorFATAL:
s.logger.logger.Fatal().Err(err.Error).Msg(err.Message)
s.logger.logger.Fatal().Str("stacktrace", fmt.Sprintf("%+v", err.Error)).Err(err.Error).Msg(err.Message)
return
case ErrorPANIC:
s.logger.logger.Panic().Err(err.Error).Msg(err.Message)
s.logger.logger.Panic().Str("stacktrace", fmt.Sprintf("%+v", err.Error)).Err(err.Error).Msg(err.Message)
return
default:
s.logger.logger.Error().Err(err.Error).Msg(err.Message)
s.logger.logger.Error().Str("stacktrace", fmt.Sprintf("%+v", err.Error)).Err(err.Error).Msg(err.Message)
}
}

View File

@@ -54,6 +54,8 @@ func (server *Server) NewMiddleware(
if herr.RenderErrorPage {
return
}
next.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, newReq)
})

View File

@@ -2,10 +2,12 @@ package hwsauth
import (
"context"
"git.haelnorr.com/h/golib/hws"
"net/http"
"slices"
"time"
"git.haelnorr.com/h/golib/hws"
"github.com/pkg/errors"
)
// Authenticate returns the main authentication middleware.
@@ -30,12 +32,20 @@ func (auth *Authenticator[T, TX]) authenticate() hws.MiddlewareFunc {
// Start the transaction
tx, err := auth.beginTx(ctx)
if err != nil {
return nil, &hws.HWSError{Message: "Unable to start transaction", StatusCode: http.StatusServiceUnavailable, Error: err}
return nil, &hws.HWSError{
Message: "Unable to start transaction",
StatusCode: http.StatusServiceUnavailable,
Error: errors.Wrap(err, "auth.beginTx"),
}
}
// Type assert to TX - safe because user's beginTx should return their TX type
txTyped, ok := tx.(TX)
if !ok {
return nil, &hws.HWSError{Message: "Transaction type mismatch", StatusCode: http.StatusInternalServerError, Error: err}
return nil, &hws.HWSError{
Message: "Transaction type mismatch",
StatusCode: http.StatusInternalServerError,
Error: errors.Wrap(err, "TX type not ok"),
}
}
model, err := auth.getAuthenticatedUser(txTyped, w, r)
if err != nil {