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:
Mechiel Lukkien
2025-03-24 13:46:08 +01:00
parent 15a8ce8c0b
commit a2c79e25c1
38 changed files with 337 additions and 161 deletions

View File

@ -13,6 +13,7 @@ import (
"time"
"github.com/mjl-/mox/dns"
"github.com/mjl-/mox/mlog"
)
var ErrNoRegistration = errors.New("registration date not found")
@ -43,7 +44,7 @@ type Bootstrap struct {
// domain through RDAP.
//
// Not all TLDs have RDAP services yet at the time of writing.
func LookupLastDomainRegistration(ctx context.Context, dom dns.Domain) (time.Time, error) {
func LookupLastDomainRegistration(ctx context.Context, log mlog.Log, dom dns.Domain) (time.Time, error) {
// ../rfc/9224:434 Against advice, we do not cache the bootstrap data. This is
// currently used by the quickstart, which is run once, or run from the cli without
// a place to keep state.
@ -57,7 +58,10 @@ func LookupLastDomainRegistration(ctx context.Context, dom dns.Domain) (time.Tim
if err != nil {
return time.Time{}, fmt.Errorf("http get of iana dns bootstrap data: %v", err)
}
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
log.Check(err, "closing http response body")
}()
if resp.StatusCode/100 != 2 {
return time.Time{}, fmt.Errorf("http get resulted in status %q, expected 200 ok", resp.Status)
}
@ -94,7 +98,7 @@ func LookupLastDomainRegistration(ctx context.Context, dom dns.Domain) (time.Tim
var lastErr error
for _, u := range urls {
var reg time.Time
reg, lastErr = rdapDomainRequest(ctx, u, dom)
reg, lastErr = rdapDomainRequest(ctx, log, u, dom)
if lastErr == nil {
return reg, nil
}
@ -126,7 +130,7 @@ type Event struct {
// rdapDomainRequest looks up a the most recent registration time of a at an RDAP
// service base URL.
func rdapDomainRequest(ctx context.Context, rdapURL string, dom dns.Domain) (time.Time, error) {
func rdapDomainRequest(ctx context.Context, log mlog.Log, rdapURL string, dom dns.Domain) (time.Time, error) {
// ../rfc/9082:316
// ../rfc/9224:177 base URLs have a trailing slash.
rdapURL += "domain/" + dom.ASCII
@ -141,7 +145,10 @@ func rdapDomainRequest(ctx context.Context, rdapURL string, dom dns.Domain) (tim
if err != nil {
return time.Time{}, fmt.Errorf("http domain rdap get request: %v", err)
}
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
log.Check(err, "closing http response body")
}()
switch {
case resp.StatusCode == http.StatusNotFound: