mirror of
https://github.com/mjl-/mox.git
synced 2025-07-13 14:54:35 +03:00
check and log errors more often in deferred cleanup calls, and log remote-induced errors at lower priority
We normally check errors for all operations. But for some cleanup calls, eg "defer file.Close()", we didn't. Now we also check and log most of those. Partially because those errors can point to some mishandling or unexpected code paths (eg file unexpected already closed). And in part to make it easier to use "errcheck" to find the real missing error checks, there is too much noise now. The log.Check function can now be used unconditionally for checking and logging about errors. It adjusts the log level if the error is caused by a network connection being closed, or a context is canceled or its deadline reached, or a socket deadline is reached.
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@ -17,6 +18,7 @@ type Client struct {
|
||||
Username string // Added as HTTP basic authentication if not empty.
|
||||
Password string
|
||||
HTTPClient *http.Client // Optional, defaults to http.DefaultClient.
|
||||
Logger *slog.Logger // Optional, defaults to slog.Default().
|
||||
}
|
||||
|
||||
var _ Methods = Client{}
|
||||
@ -28,12 +30,24 @@ func (c Client) httpClient() *http.Client {
|
||||
return http.DefaultClient
|
||||
}
|
||||
|
||||
func (c Client) log(ctx context.Context) *slog.Logger {
|
||||
if c.Logger != nil {
|
||||
return c.Logger
|
||||
}
|
||||
return slog.Default()
|
||||
}
|
||||
|
||||
func transact[T any](ctx context.Context, c Client, fn string, req any) (resp T, rerr error) {
|
||||
hresp, err := httpDo(ctx, c, fn, req)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
defer hresp.Body.Close()
|
||||
defer func() {
|
||||
err := hresp.Body.Close()
|
||||
if err != nil {
|
||||
c.log(ctx).Debug("closing http response body", slog.Any("err", err))
|
||||
}
|
||||
}()
|
||||
|
||||
if hresp.StatusCode == http.StatusOK {
|
||||
// Text and HTML of a message can each be 1MB. Another MB for other data would be a
|
||||
@ -52,7 +66,10 @@ func transactReadCloser(ctx context.Context, c Client, fn string, req any) (resp
|
||||
body := hresp.Body
|
||||
defer func() {
|
||||
if body != nil {
|
||||
body.Close()
|
||||
err := body.Close()
|
||||
if err != nil {
|
||||
c.log(ctx).Debug("closing http response body", slog.Any("err", err))
|
||||
}
|
||||
}
|
||||
}()
|
||||
if hresp.StatusCode == http.StatusOK {
|
||||
|
Reference in New Issue
Block a user