Return sink write errors from every handler (closes #22)
The console and JSON handlers threw away the error from their write to stdout, so a lost log line looked delivered. They now return it, wrapped. The webhook handler also returns an error for a status outside 2xx, and no longer follows redirects, which could resend the request without the record. The multiplex handler passes the record to every handler and returns their errors joined with errors.Join instead of stopping at the first. Both stdout handlers gain an unexported writer, nil meaning os.Stdout at write time, so tests can supply a sink that fails. The README says what each handler returns and that the slog.Logger methods discard a handler's error. Model: opus-5-5
This commit is contained in:
+8
-4
@@ -7,6 +7,7 @@ package simplelog
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
@@ -62,20 +63,23 @@ func NewMultiplexHandler() slog.Handler {
|
||||
return cl
|
||||
}
|
||||
|
||||
// Handle forwards the record to every underlying handler, stopping at
|
||||
// the first error.
|
||||
// Handle forwards the record to every underlying handler, including the
|
||||
// ones after a handler that fails. It returns the failures joined with
|
||||
// errors.Join, or nil when every handler succeeded.
|
||||
func (cl *MultiplexHandler) Handle(
|
||||
ctx context.Context,
|
||||
record slog.Record,
|
||||
) error {
|
||||
var errs []error
|
||||
|
||||
for _, handler := range cl.handlers {
|
||||
err := handler.Handle(ctx, record)
|
||||
if err != nil {
|
||||
return err
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// Enabled reports whether the handler processes records at the given
|
||||
|
||||
Reference in New Issue
Block a user