strudel

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2021 License: MIT Imports: 8 Imported by: 0

README

Strudel

Build Status codecov Go Report Card

Strudel provides structured error handling and logging middleware for use with Janice handlers. It uses the excellent Logrus, jsend and httpsnoop packages.

The package is intended to help reduce the initial middleware boilerplate when building an HTTP API. Realistically, error handing and logging requirements will change as an app develops, so the intention here is to simply provide drop in/out components that allow rapid prototyping.

Getting started

go get github.com/stevecallear/strudel

Example

package main

import (
	"net/http"

	"github.com/stevecallear/janice"
	"github.com/stevecallear/strudel"
)

func main() {
	chain := janice.New(strudel.ErrorHandling)

	mux := http.NewServeMux()
	mux.Handle("/", chain.Then(func(w http.ResponseWriter, r *http.Request) error {
		return strudel.NewError("resource not found").
			WithCode(http.StatusNotFound).
			WithField("resourceId", "abc123").
			WithLogField("sensitiveId", "cde456")
	}))

	h := janice.New(strudel.RequestTracking, strudel.Recovery, strudel.RequestLogging).Then(janice.Wrap(mux))
	http.ListenAndServe(":8080", h)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Logger is the logger used for all middleware
	Logger *logrus.Logger

	// GetRequestID returns the id for the specified request
	GetRequestID = func(r *http.Request) (string, bool) {
		v, _ := r.Context().Value(reqIDKey).(string)
		return v, v != ""
	}
)

Functions

func ErrorHandling

func ErrorHandling(n janice.HandlerFunc) janice.HandlerFunc

ErrorHandling is an error handling middleware function

func Recovery

Recovery is a panic recovery middleware function

func RequestLogging

func RequestLogging(n janice.HandlerFunc) janice.HandlerFunc

RequestLogging is a request logging middleware function

func RequestTracking

func RequestTracking(n janice.HandlerFunc) janice.HandlerFunc

RequestTracking is a request tracking middleware function

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error represents an error

func NewError

func NewError(msg string) *Error

NewError returns a new error

func (*Error) Code

func (e *Error) Code() int

Code returns the error code

func (*Error) Error

func (e *Error) Error() string

Error returns the error message

func (*Error) Fields

func (e *Error) Fields() Fields

Fields returns all error fields that are not log-only

func (*Error) LogFields

func (e *Error) LogFields() Fields

LogFields returns all error fields including those that are log-only

func (*Error) WithCode

func (e *Error) WithCode(code int) *Error

WithCode sets the error code

func (*Error) WithField

func (e *Error) WithField(key string, value interface{}) *Error

WithField adds the specified error field

func (*Error) WithFields

func (e *Error) WithFields(f Fields) *Error

WithFields adds the specified error fields

func (*Error) WithLogField

func (e *Error) WithLogField(key string, value interface{}) *Error

WithLogField adds the specified log-only error field

func (*Error) WithLogFields

func (e *Error) WithLogFields(f Fields) *Error

WithLogFields adds the specified log-only error fields

type Fields

type Fields map[string]interface{}

Fields represents a set of error fields

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL