handler

package
v0.0.0-...-58d3f0e Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2017 License: MIT Imports: 9 Imported by: 0

README

API Errors Handler

Handles errors sent by handy go-apierr package.

Install

go get github.com/mlanin/iris-middlewares/apierr-handler

Can

  • Recover APIErrors and send them right to the user.
  • Handles APIError's context and trace options.

Also it can transform all uncached panic errors into InternalServerError and saves them to logs.

If your environment is set to production, user will never see your panics. If you want to see them, set your env to anything else like develop by EnvGetter option.

By default only InternalServerErrors are saved to log. If you want to save every error, set debug mode to true by DebugGetter.

Usage

import (
  "github.com/kataras/iris"
  handler "github.com/mlanin/iris-middlewares/apierr-handler"
)

func main() {
  errorsHandler := handler.New(handler.Config{
    // Set your environment.
    EnvGetter: func() string {
      return "production"
    },
    // Set your debug mode.
    DebugGetter: func() bool {
      return false
    },
  })

  iris.Use(errorsHandler)
}

Working example

Full working example can be found in API Boilerplate

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	EnvGetter   func() string
	DebugGetter func() bool
}

Config for the Handler.

type Handler

type Handler struct {
	Config Config
}

Handler for APIErrors.

func New

func New(cfg Config) *Handler

New restores the server on internal server errors (panics) returns the middleware

is here for compatiblity

func (*Handler) NewAPIError

func (h *Handler) NewAPIError(err error) *apierr.APIError

NewAPIError makes new API error.

func (*Handler) Serve

func (h *Handler) Serve(ctx *iris.Context)

Serve the middleware.

Jump to

Keyboard shortcuts

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