Codebase list golang-github-go-kit-kit / run/dff50435-7a33-4f0c-bbdc-f455eb10d80a/main transport / error_handler.go
run/dff50435-7a33-4f0c-bbdc-f455eb10d80a/main

Tree @run/dff50435-7a33-4f0c-bbdc-f455eb10d80a/main (Download .tar.gz)

error_handler.go @run/dff50435-7a33-4f0c-bbdc-f455eb10d80a/mainraw · history · blame

package transport

import (
	"context"

	"github.com/go-kit/log"
)

// ErrorHandler receives a transport error to be processed for diagnostic purposes.
// Usually this means logging the error.
type ErrorHandler interface {
	Handle(ctx context.Context, err error)
}

// LogErrorHandler is a transport error handler implementation which logs an error.
type LogErrorHandler struct {
	logger log.Logger
}

func NewLogErrorHandler(logger log.Logger) *LogErrorHandler {
	return &LogErrorHandler{
		logger: logger,
	}
}

func (h *LogErrorHandler) Handle(ctx context.Context, err error) {
	h.logger.Log("err", err)
}

// The ErrorHandlerFunc type is an adapter to allow the use of
// ordinary function as ErrorHandler. If f is a function
// with the appropriate signature, ErrorHandlerFunc(f) is a
// ErrorHandler that calls f.
type ErrorHandlerFunc func(ctx context.Context, err error)

// Handle calls f(ctx, err).
func (f ErrorHandlerFunc) Handle(ctx context.Context, err error) {
	f(ctx, err)
}