Skip to content

Commit

Permalink
feat(httperror): Set up a default func for ignoreable error
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Aug 26, 2024
1 parent 8b02d9d commit 18bce45
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/cache/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package cache

import (
"context"
"errors"
"fmt"
"log/slog"

"github.com/ViBiOh/httputils/v4/pkg/concurrent"
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/telemetry"
"go.opentelemetry.io/otel/trace"
)
Expand Down Expand Up @@ -168,7 +168,7 @@ func (c *Cache[K, V]) redisValues(ctx context.Context, ids []K) ([]string, []str
values, err := c.read.LoadMany(loadCtx, keys...)
if err != nil {
level := slog.LevelError
if errors.Is(err, context.Canceled) {
if httperror.CanBeIgnored(err) {
level = slog.LevelWarn
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/httperror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"log/slog"
"net/http"
"syscall"

"github.com/ViBiOh/httputils/v4/pkg/model"
)
Expand All @@ -13,6 +14,14 @@ const (
internalError = "Oops! Something went wrong. Server's logs contain more details."
)

func CanBeIgnored(err error) bool {
if err == nil {
return true
}

return errors.Is(err, syscall.EPIPE) || errors.Is(err, syscall.ECONNRESET) || errors.Is(err, context.Canceled)
}

func httpError(ctx context.Context, w http.ResponseWriter, status int, payload string, err error) {
w.Header().Add("Cache-Control", "no-cache")
http.Error(w, payload, status)
Expand Down
4 changes: 2 additions & 2 deletions pkg/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pprof
import (
"bytes"
"context"
"errors"
"flag"
"fmt"
"log/slog"
Expand All @@ -14,6 +13,7 @@ import (
"time"

"github.com/ViBiOh/flags"
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/recoverer"
"github.com/ViBiOh/httputils/v4/pkg/request"
)
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *Service) push(ctx context.Context) {
case <-ctx.Done():
return
case <-ticker.C:
if err := s.send(ctx); err != nil && !errors.Is(err, context.Canceled) {
if err := s.send(ctx); !httperror.CanBeIgnored(err) {
slog.LogAttrs(ctx, slog.LevelError, "pprof export", slog.Any("error", err))
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/ViBiOh/flags"
"github.com/ViBiOh/httputils/v4/pkg/httperror"
"github.com/ViBiOh/httputils/v4/pkg/model"
"github.com/ViBiOh/httputils/v4/pkg/recoverer"
"github.com/redis/go-redis/extra/redisotel/v9"
Expand Down Expand Up @@ -115,7 +116,7 @@ func (s *Service) Load(ctx context.Context, key string) ([]byte, error) {
return content, nil
}

if err != redis.Nil && !errors.Is(err, context.Canceled) {
if err != redis.Nil && !httperror.CanBeIgnored(err) {
return nil, fmt.Errorf("exec get: %w", err)
}

Expand Down

0 comments on commit 18bce45

Please sign in to comment.