Giter VIP home page Giter VIP logo

go-fnerror's Introduction

FINNOMENA ERROR

MIT License GoDoc

FINNOMENA Error is an extension of standard golang error which provides more flexibility to use with additional information about an error (Ex. stack tracing)

Prerequisites

Golang 1.13 or further version must be installed, in order to use the standard library functions such as errors.Is() or errors.As() to examine a child error in the error chain.

Installation

go get github.com/finnomena/go-fnerror

Example

package main

import (
	"errors"
	"fmt"

	"github.com/finnomena/go-fnerror"
)

func someFunction() fnerror.Apperror {
	return fnerror.NewUnexpectedError("CUSTOM MESSAGE", nil)
}

func main() {
	unexpectedErr := fnerror.NewUnexpectedError("", nil)
	notFoundErr := fnerror.NewNotFoundError("", unexpectedErr)
	fmt.Println(notFoundErr.Error())
	// Not Found
	fmt.Println(notFoundErr.GetCode())
	// 101 --> application error code
	fmt.Println(notFoundErr.GetHTTPCode())
	// 404 --> http response code
	fmt.Println(notFoundErr.GetTrace())
	// {$dir}/main.go:16
	fmt.Println(fnerror.GetChainTrace(notFoundErr))
	/*
		main.main() "Not Found"
		{$dir}/main.go:16

		main.main() "CUSTOM MESSAGE"
		{$dir}/main.go:15
	*/
	standardErr := errors.New("Error")
	unexpectedErr2 := new(fnerror.UnexpectedError) //allocate with new only for type assertion
	forbiddenErr := new(fnerror.ForbiddenError)

	fmt.Println(errors.Is(notFoundErr, standardErr))
	//false --> has no standard error (compare to a value)
	fmt.Println(errors.Is(notFoundErr, unexpectedErr))
	//true --> has unexpectedErr
	fmt.Println(errors.Is(notFoundErr, unexpectedErr2))
	//false --> doesn't has unexpectedErr2
	fmt.Println(errors.Is(notFoundErr, forbiddenErr))
	//false --> doesnt has forbiddenErr

	fmt.Println(errors.As(notFoundErr, &standardErr))
	//true --> contains error type (compare to a type)
	fmt.Println(errors.As(notFoundErr, &unexpectedErr2))
	//true --> contains UnexpectedError type
	fmt.Println(errors.As(notFoundErr, &forbiddenErr))
	//false --> contains ForbiddenError type
}

Contributing

  • Use PR for a small change (including test case and doc)
  • For a major change, open an issue for dicussion first.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.