GithubHelp home page GithubHelp logo

sollniss / resperr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from earthboundkid/resperr

1.0 0.0 0.0 17 KB

Go package to associate status codes and messages with errors

License: MIT License

Go 100.00%
error go golang

resperr's Introduction

resperr

Resperr is a Go package to associate status codes and messages with errors. This package is forked from https://godoc.org/github.com/carlmjohnson/resperr. Compared to the original, this package allows to customize the default status codes and error messages. It also provides a more flexible API.

Example usage

Attaching information to an error

if err != nil {
	// Attach message and status code.
	return resperr.WithCodeAndMessage(err, http.StatusBadRequest, "You did something wrong.")
}

if err != nil {
	// Attach status code.
	return resperr.WithStatusCode(err, http.StatusBadRequest)
}

if err != nil {
	// Attach message.
	return resperr.WithUserMessage(err, "Could not fetch data.")
}

if !ok {
	// Generate a new error with a message attached.
	return resperr.WithUserMessage(nil, "Something is not ok.")
}

Getting information from an error

func writeError(w http.ResponseWriter, err error) {
	// Default status code and message can be specified.
	w.WriteHeader(resperr.StatusCode(err))
	w.Write([]byte(`{"error":"` + resperr.UserMessage(err) + `"}`))
}

resperr's People

Contributors

earthboundkid avatar sollniss avatar

Stargazers

Sevdalin Sabev avatar

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.