Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab3d7b62a9 |
@@ -115,8 +115,7 @@ the record:
|
||||
- `ConsoleHandler` and `JSONHandler` return the error from their write to
|
||||
stdout, wrapped, so `errors.Is` still matches the original
|
||||
- `WebhookHandler` returns an error when the request fails, and also when
|
||||
the server answers with a status outside 2xx, a redirect included,
|
||||
since it does not follow redirects
|
||||
the server answers with a status outside 2xx
|
||||
- `MultiplexHandler`, which simplelog installs as the default, passes the
|
||||
record to every handler it holds even after one of them fails, then
|
||||
returns all their errors joined with `errors.Join` (nil if none failed)
|
||||
|
||||
@@ -24,7 +24,7 @@ files it depends on: .golangci.yml, REPO_POLICIES.md, .editorconfig,
|
||||
|
||||
# Completed Steps
|
||||
|
||||
* 2026-10-06: every handler now returns a failed delivery from `Handle`
|
||||
* 2026-10-06: every handler now returns a failed delivery from Handle
|
||||
instead of discarding it: console and JSON return the stdout write
|
||||
error, the webhook also fails on a non-2xx answer, and the multiplex
|
||||
delivers to every handler and returns their errors joined
|
||||
|
||||
@@ -134,29 +134,3 @@ func TestWebhookHandlerReturnsErrorOnServerError(t *testing.T) {
|
||||
t.Fatalf("Handle returned %v, want an error for the 500 answer", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Following the redirect would resend the request as a GET without the
|
||||
// record, and the GET is answered with 200, so only an error for the
|
||||
// redirect itself tells the caller the record was lost.
|
||||
func TestWebhookHandlerReturnsErrorOnRedirect(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/moved" {
|
||||
http.Redirect(w, r, "/moved", http.StatusFound)
|
||||
}
|
||||
},
|
||||
))
|
||||
defer server.Close()
|
||||
|
||||
handler, err := NewWebhookHandler(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewWebhookHandler: %v", err)
|
||||
}
|
||||
|
||||
err = handler.Handle(context.Background(), errorTestRecord())
|
||||
if !errors.Is(err, errWebhookStatus) {
|
||||
t.Fatalf("Handle returned %v, want an error for the redirect", err)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-15
@@ -19,7 +19,6 @@ var errWebhookStatus = errors.New("webhook did not accept the record")
|
||||
// URL.
|
||||
type WebhookHandler struct {
|
||||
webhookURL string
|
||||
client *http.Client
|
||||
attrs handlerAttrs
|
||||
}
|
||||
|
||||
@@ -31,17 +30,7 @@ func NewWebhookHandler(webhookURL string) (*WebhookHandler, error) {
|
||||
return nil, fmt.Errorf("invalid webhook URL: %w", err)
|
||||
}
|
||||
|
||||
return &WebhookHandler{
|
||||
webhookURL: webhookURL,
|
||||
client: &http.Client{
|
||||
// Following a redirect can resend the request as a GET
|
||||
// without the record, so Handle gets the redirect answer
|
||||
// itself and returns it as an error.
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
return &WebhookHandler{webhookURL: webhookURL}, nil
|
||||
}
|
||||
|
||||
// Enabled reports whether the handler processes records at the given
|
||||
@@ -59,7 +48,6 @@ func (w *WebhookHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
|
||||
return &WebhookHandler{
|
||||
webhookURL: w.webhookURL,
|
||||
client: w.client,
|
||||
attrs: w.attrs.withAttrs(attrs),
|
||||
}
|
||||
}
|
||||
@@ -73,7 +61,6 @@ func (w *WebhookHandler) WithGroup(name string) slog.Handler {
|
||||
|
||||
return &WebhookHandler{
|
||||
webhookURL: w.webhookURL,
|
||||
client: w.client,
|
||||
attrs: w.attrs.withGroup(name),
|
||||
}
|
||||
}
|
||||
@@ -99,7 +86,7 @@ func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response, err := w.client.Do(request)
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user