gorrors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

Gorrors

Build Status

Simple error handling for Golang. The traditional error handling in Go is like the below.

if err != nil {
    return err
}

This error message doesn't include and context or debugging information.

Features

  • Compatible with Go's error
  • Call Stack Traces
  • HTTP response status code
  • Render error in JSON

Installation

go get github.com/erdaltsksn/gorrors

Usage

package main

import (
	"net/http"
	
	"github.com/erdaltsksn/gorrors"
)

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	gorrors.SetHTTPStatus(http.StatusInternalServerError)
	err := gorrors.New("Custom Error message")
	gorrors.RenderJSON(w, r, err)
}

Contributing

If you want to contribute to this project and make it better, your help is very welcome. See CONTRIBUTING for more information.

Disclaimer

In no event shall we be liable to you or any third parties for any special, punitive, incidental, indirect or consequential damages of any kind, or any damages whatsoever, including, without limitation, those resulting from loss of use, data or profits, and on any theory of liability, arising out of or in connection with the use of this software.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderJSON

func RenderJSON(w http.ResponseWriter, r *http.Request, err *Error)

RenderJSON marshals `goerr.Error` to `JSON`, automatically excaping HTML and sets the 'Content-Type' as `application/json`.

func SetHTTPStatus

func SetHTTPStatus(code int)

SetHTTPStatus sets a HTTP response status code if it's not set before.

Types

type Error

type Error struct {
	Message string  `json:"message,omitempty"`
	Details []error `json:"details,omitempty"`
	Trace   trace   `json:"trace,omitempty"`
}

Error is a simple error struct with a HTTP response status code. SEE: https://www.restapitutorial.com/httpstatuscodes.html

func New

func New(message string, details ...error) *Error

New returns a new `goerr.Error` with given values.

func (*Error) Error

func (e *Error) Error() string

Error implement Error() method that makes `Error` usable in return argument.

Jump to

Keyboard shortcuts

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