All Projects → pjebs → jsonerror

pjebs / jsonerror

Licence: MIT License
Makes Go error-handling a breeze!

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to jsonerror

Bugsnag Android Ndk
DEPRECATED - this project now lives at bugsnag/bugsnag-android
Stars: ✭ 42 (+50%)
Mutual labels:  errors, error-handling
Bugsnag Cocoa
Bugsnag crash reporting for iOS, macOS and tvOS apps
Stars: ✭ 167 (+496.43%)
Mutual labels:  errors, error-handling
Bugsnag Node
[DEPRECATED] Please upgrade to our Universal JS notifier "@bugsnag/js" • https://github.com/bugsnag/bugsnag-js
Stars: ✭ 48 (+71.43%)
Mutual labels:  errors, error-handling
Bugsnag Laravel
Bugsnag notifier for the Laravel PHP framework. Monitor and report Laravel errors.
Stars: ✭ 746 (+2564.29%)
Mutual labels:  errors, error-handling
fail
Better error handling solution specially designed for web application servers
Stars: ✭ 27 (-3.57%)
Mutual labels:  errors, error-handling
Eris
eris provides a better way to handle, trace, and log errors in Go 🎆
Stars: ✭ 758 (+2607.14%)
Mutual labels:  errors, error-handling
Bugsnag Go
Automatic panic monitoring for Go and Go web frameworks, like negroni, gin, and revel
Stars: ✭ 155 (+453.57%)
Mutual labels:  errors, error-handling
Traceback with variables
Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works in Jupyter and IPython. Install with pip or conda.
Stars: ✭ 509 (+1717.86%)
Mutual labels:  errors, error-handling
errors
errors with paired message and caller stack frame
Stars: ✭ 19 (-32.14%)
Mutual labels:  errors, error-handling
Bugsnag Ruby
Bugsnag error monitoring & reporting software for rails, sinatra, rack and ruby
Stars: ✭ 211 (+653.57%)
Mutual labels:  errors, error-handling
Errorx
A comprehensive error handling library for Go
Stars: ✭ 712 (+2442.86%)
Mutual labels:  errors, error-handling
rakered
The open source components from rake.red
Stars: ✭ 28 (+0%)
Mutual labels:  errors, error-handling
Tracerr
Golang errors with stack trace and source fragments.
Stars: ✭ 646 (+2207.14%)
Mutual labels:  errors, error-handling
failure
An error handling package for Go.
Stars: ✭ 24 (-14.29%)
Mutual labels:  errors, error-handling
Reattempt
🤞 Give your functions another chance
Stars: ✭ 570 (+1935.71%)
Mutual labels:  errors, error-handling
Elmahcore
ELMAH for Net.Standard and Net.Core
Stars: ✭ 127 (+353.57%)
Mutual labels:  errors, error-handling
Bugsnag Php
Bugsnag error monitoring and crash reporting tool for PHP apps
Stars: ✭ 475 (+1596.43%)
Mutual labels:  errors, error-handling
Rollbar.js
Error tracking and logging from JS to Rollbar
Stars: ✭ 479 (+1610.71%)
Mutual labels:  errors, error-handling
Whoops
PHP errors for cool kids
Stars: ✭ 12,646 (+45064.29%)
Mutual labels:  errors, error-handling
bugsnag-java
Bugsnag error reporting for Java.
Stars: ✭ 51 (+82.14%)
Mutual labels:  errors, error-handling

JSONError for Golang GoDoc

This package is for adding some structure to your error messages. This makes error-handling, debugging and diagnosis for all your Go projects a lot more elegant and simpler. Use it wherever error type is required.

It utilizes the fact that built-in type error is actually an interface.

The package also contains the ErrorCollection struct which allows for accumulation of multiple errors. It is safe to use from multiple concurrent goroutines unlike other comparable packages.

Please Star this package so I can add it to awesome-go.

Refer to documentation on GoDoc because the information below is a small subset of all the features.

Install

go get -u github.com/pjebs/jsonerror

Optional - if you want to output JSON formatted error messages (e.g. for REST API):

go get -u gopkg.in/unrolled/render.v1

Prehistoric Usage - Using Go Standard Library

import (
	"errors"
	"fmt"
)

func main() {
	err := errors.New("emit macho dwarf: elf header corrupted")
	if err != nil {
		fmt.Print(err)
	}
}

//Or alternatively

panic(errors.New("failed"))

Using this package instead

import (
	errors "github.com/pjebs/jsonerror" //aliased for ease of usage
	"math"                              //For realSquareRoot() example function below
)

//EXAMPLE 1 - Creating a JE Struct

err := errors.New(1, "Square root of negative number is prohibited", "Please make number positive or zero") //Domain is optional and not included here

//Or  
err := errors.New(1, "Square root of negative number is prohibited", "Please make number positive or zero", "com.github.pjebs.jsonerror")

//EXAMPLE 2 - Practical Example

//Custom function
func realSquareRoot(n float64) (float64, error) {
	if n < 0 {
		return 0, errors.New(1, "Square root of negative number is prohibited", "Please make number positive or zero")
	} else {
		return math.Sqrt(n), nil
	}
}

//A function that uses realSquareRoot
func main() {

	s, err := realSquareRoot(12.0)
	if err != nil {
		if err.(errors.JE).Code == 1 {
			//Square root of negative number
		} else {
			//Unknown error
		}
		return
	}

	//s is Valid answer
}

Methods

func New(code int, error string, message string, domain ...string) JE

code int - Error code. Arbitrary and set by fiat. Different types of errors should have an unique error code in your project.

error string - A standard description of the error code.

message string - A more detailed description that may be customized for the particular circumstances. May also provide extra information.

domain ...string - Optional It allows you to distinguish between same error codes. Only 1 domain string is allowed.

func (this JE) Render() map[string]string {

Formats JE (JSONError) struct so it can be used by gopkg.in/unrolled/render.v1 package to generate JSON output.

Output JSON formatted error message (i.e. REST API Server response)

import (
	"github.com/codegangsta/negroni" //Using Negroni (https://github.com/codegangsta/negroni)
	errors "github.com/pjebs/jsonerror"
	"gopkg.in/unrolled/render.v1"
	"net/http"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {

		err := errors.New(12, "Unauthorized Access", "Please log in first to access this site")

    	r := render.New(render.Options{})
		r.JSON(w, http.StatusUnauthorized, err.Render())
		return
  	
  	})

  	n := negroni.Classic()
  	n.UseHandler(mux)
  	n.Run(":3000")
}

For the above example, the web server will respond with a HTTP Status Code of 401 (Status Unauthorized), Content-Type as application/json and a JSON response:

{"code":"12","error":"Unauthorized Access","message":"Please log in first to access this site"}

FAQ

What is the domain parameter?

The domain parameter is optional. It allows you to distinguish between same error codes. That way different packages (or different parts of your own project) can use the same error codes (for different purposes) and still be differentiated by the domain identifier.

NB: The domain parameter is not outputted by Render() (for generating JSON formatted output)

How do I use this package?

When you want to return an error (e.g. from a function), just return a JE struct. See the example code above.

Or you can use it with panic().

panic(jsonerror.New(1, "error", "message"))

What are the error codes?

You make them up for your particular project! By fiat, you arbitrarily set each error code to mean a different type of error.

Final Notes

If you found this package useful, please Star it on github. Feel free to fork or provide pull requests. Any bug reports will be warmly received.

PJ Engineering and Business Solutions Pty. Ltd.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].