stackerr

package module
Version: v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2021 License: MIT Imports: 6 Imported by: 0

README

StackErr

CircleCI Go Report Card codecov GoDoc

An error implementation with StatusCode and Stacktrace

It implements the Golang error interface

You can use Status Code to better identify the answer you need to report to the client.

Makes debugging easier by logging the functions the error passes through and by adding the ability to log context on each function pass of the error, so that you can create a path of the error through your application.

Install

$ go get github.com/efimovalex/stackerr

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/efimovalex/stackerr"
)

func f1() *stackerr.Err {
	err := stackerr.NewWithStatusCode("message", http.StatusNotFound)
	return err.Stack()
}

func f2() *stackerr.Err {
	err := f1()
	return err.StackWithContext("context")
}

type t1 struct{}

func (t *t1) f3() *stackerr.Err {
	err := f2()
	return err.Stack()
}

func main() {
	ts := t1{}
	err := ts.f3()

	fmt.Println(err.Sprint())

	fmt.Println(err.Error())

	fmt.Println(err.StatusCode)

	if err.IsNotFound() {
		fmt.Println("Resource is not found")
	}

	err.Log()
}

Output:

Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:29 (main.main) 
-> /home/efi/workspace/stackerr/example/main.go:24 (main.(*t1).f3) 
-> /home/efi/workspace/stackerr/example/main.go:17 (main.f2) context
-> /home/efi/workspace/stackerr/example/main.go:11 (main.f1) 

message
404
Resource is not found

2021/10/26 17:58:11 Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:29 (main.main) 
-> /home/efi/workspace/stackerr/example/main.go:24 (main.(*t1).f3) 
-> /home/efi/workspace/stackerr/example/main.go:17 (main.f2) context
-> /home/efi/workspace/stackerr/example/main.go:11 (main.f1) 

2021/10/26 17:58:11 Error Stacktrace:
-> /home/efi/workspace/stackerr/example/main.go:41 (main.main) 

Authors

Created and maintained by

Efimov Alex - @efimovalex

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Err

type Err struct {
	Message    string
	StatusCode int
	// contains filtered or unexported fields
}

Err error

func New added in v1.0.2

func New(message string) *Err

New - creates a new Err struct with 500 Error Code

func NewFromError added in v1.0.2

func NewFromError(e error) *Err

NewFromError - creates a new Err struct with a custom Error Code

func NewWithStatusCode added in v1.0.2

func NewWithStatusCode(message string, statusCode int) *Err

NewWithStatusCode - creates a new Err struct with a custom Error Code

func (*Err) Error

func (e *Err) Error() string

func (*Err) IsNotFound added in v1.0.2

func (e *Err) IsNotFound() bool

IsNotFound - return true if the error type is resource not found.

func (*Err) Log

func (e *Err) Log()

Log - logs the Stacktrace using the native log package

func (*Err) Print

func (e *Err) Print()

Print - prints the Stacktrace

func (*Err) Sprint

func (e *Err) Sprint() string

Sprint - returns a pretty printed string of the Stacktrace ready for printng

func (*Err) Stack

func (e *Err) Stack() *Err

Stack - Adds the current function to Err and a link to the previous Stacktrace

func (*Err) StackWithContext added in v1.0.2

func (e *Err) StackWithContext(context string) *Err

StackWithContext - Adds the current function and context to Err and a link to the previous Stacktrace

type Stack

type Stack struct {
	File          string
	Line          int
	Function      string
	Context       string
	CallbackStack *Stack
}

Stack - contains stack info

func (*Stack) Sprint

func (s *Stack) Sprint() string

Sprint returns a pretty printed string of the Stacktrace ready for printng

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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