blaze

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 4 Imported by: 7

README

Go Report Card GoDoc GoCover

blaze

Making golang easier

Error Handling

errHandler := blaze.NewErrorHandler()

errHandler.AddHandler(func(err error, data ...interface{}) interface{} {
    fmt.Println("Generic Error Occured")
    return SomeData
})

errHandler.AddHandler(func(err error, data ...interface{}) interface{} {
    fmt.Println("Error Of Type CustomErrorType Occured")
    return SomeData
}, CustomErrorType{})

SomeData := errHandler.Check(err, SomeRandomData1, SomeRandomData2)
// Data passed after the err will be passed to handlers

Panic Handling

func handler(msg interface{}, data ...interface{}) {
    fmt.Println("Panic Occured")
    // msg is the panic message
    // data is data passed when panic was checked
}


panicHandler := blaze.NewPanicHandler(handler)

defer panicHandler.Check("some data")
// "some data" will be passed on to the handler function

panic("some reason")

Function Status Logging

As of now, this only supports zap logger


log := zap.NewDevelopement(
        zap.AddCallerSkip(1), 
        /* remember to add 1 call skip to your zap logger
        or else the caller would always be blaze */
    )

func someFunction(someArgument string) {
    

    funcLog := blaze.NewFuncLog(
        "someFunction", // function name
        log, // zap logger
        zap.String("someArgument", someArgument), // you can provide as many zap fields
    )

    defer func(){
        if r:=recover();r!=nil {
            funcLog.Panic(r) // logs the panic message PANIC
            // Works better with panic handler of blaze
        }
    }()

    funcLog.Started() // logs that function started DEBUG

    output, err := someTask()
    if err != nil {
        wrappedErr := funcLog.Error(err) // logs the error ERROR
        // also returns an wrapped version of the error
        return wrappedErr
    }

    funcLog.Completed(zap.String("output", output)) 
    /* logs that function completed INFO.
       In all of the function of FuncLog 
       you can enter more zap fields like done here
    */
    return nil
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorHandler

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

ErrorHandler is used for error handling

func NewErrorHandler

func NewErrorHandler(
	defaultHandler func(error, ...interface{}) interface{}) *ErrorHandler

NewErrorHandler is used to create a new error handler

func (*ErrorHandler) AddHandler

func (errorHandler *ErrorHandler) AddHandler(
	handlerFunc func(error, ...interface{}) interface{},
	err error)

AddHandler adds a function handler. for the err type of err provided

func (ErrorHandler) Check

func (errorHandler ErrorHandler) Check(err error, data ...interface{}) interface{}

Check will check for error and run the handler of that error's type

func (*ErrorHandler) Init

func (errorHandler *ErrorHandler) Init()

Init initializes

type FuncLog

type FuncLog struct {
	Log *zap.Logger
	// contains filtered or unexported fields
}

FuncLog is used to easily log golang function statuses

func NewFuncLog

func NewFuncLog(name string, log *zap.Logger, fields ...zap.Field) *FuncLog

NewFuncLog will create a new func log

func (FuncLog) Completed

func (funcLog FuncLog) Completed(fields ...zap.Field)

Completed will that function completed at INFO level

func (FuncLog) Error

func (funcLog FuncLog) Error(err error, fields ...zap.Field) error

ErrorWrap will log the error at ERROR level and wrap it with information

func (FuncLog) Panic

func (funcLog FuncLog) Panic(message interface{}, fields ...zap.Field)

Panic will log the panic at PANIC level

func (FuncLog) Started

func (funcLog FuncLog) Started(fields ...zap.Field)

Started will log that function started at DEBUG level

type PanicHandler

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

PanicHandler will be used to handle panics

func NewPanicHandler

func NewPanicHandler(handler func(interface{}, ...interface{})) *PanicHandler

NewPanicHandler is used to create a new panic handler

func (PanicHandler) Check

func (panicHandler PanicHandler) Check(data interface{})

Check will check for panic and execute handler with data

Jump to

Keyboard shortcuts

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