README

resperr GoDoc Go Report Card Calver v0.YY.Minor

Go package to associate status codes and messages with errors

Documentation

Overview

    Package resperr contains helpers for associating http status codes and user messages with errors

    Example
    Output:
    
    logged   ?: [403] bad user ""
    response ?: {"status":403,"message":"Forbidden"}
    logged   ?user=admin: [400] missing ?n= in query
    response ?user=admin: {"status":400,"message":"Please enter a number."}
    logged   ?user=admin&n=x: [400] strconv.Atoi: parsing "x": invalid syntax
    response ?user=admin&n=x: {"status":400,"message":"Input is not a number."}
    logged   ?user=admin&n=1: [404] 1 not found
    response ?user=admin&n=1: {"status":404,"message":"Not Found"}
    logged   ?user=admin&n=2: could not connect to database (X_X)
    response ?user=admin&n=2: {"status":500,"message":"Internal Server Error"}
    response ?user=admin&n=3: {"data":"data 3"}
    

    Index

    Examples

    Constants

    This section is empty.

    Variables

    This section is empty.

    Functions

    func New

    func New(code int, format string, v ...interface{}) error

      New is a convenience function for calling fmt.Errorf and WithStatusCode.

      func NotFound

      func NotFound(r *http.Request) error

        NotFound creates an error with a 404 status code and a user message showing the request path that was not found.

        func StatusCode

        func StatusCode(err error) (code int)

          StatusCode returns the status code associated with an error. If no status code is found, it returns 500 http.StatusInternalServerError. As a special case, it checks for Timeout() and Temporary() errors and returns 504 http.StatusGatewayTimeout and 503 http.StatusServiceUnavailable respectively. If err is nil, it returns 200 http.StatusOK.

          func UserMessage

          func UserMessage(err error) string

            UserMessage returns the user message associated with an error. If no message is found, it checks StatusCode and returns that message. Because the default status is 500, the default message is "Internal Server Error". If err is nil, it returns "".

            func WithCodeAndMessage

            func WithCodeAndMessage(err error, code int, msg string) error

              WithCodeAndMessage is a convenience function for calling both WithStatusCode and WithUserMessage.

              func WithStatusCode

              func WithStatusCode(err error, code int) error

                WithStatusCode adds a StatusCoder to err's error chain. Unlike pkg/errors, WithStatusCode will wrap nil error.

                func WithUserMessage

                func WithUserMessage(err error, msg string) error

                  WithUserMessage adds a UserMessenger to err's error chain. Unlike pkg/errors, WithUserMessage will wrap nil error.

                  func WithUserMessagef

                  func WithUserMessagef(err error, format string, v ...interface{}) error

                    WithUserMessagef calls fmt.Sprintf before calling WithUserMessage.

                    Types

                    type StatusCoder

                    type StatusCoder interface {
                    	error
                    	StatusCode() int
                    }

                      StatusCoder is an error with an associated HTTP status code

                      type UserMessenger

                      type UserMessenger interface {
                      	error
                      	UserMessage() string
                      }

                        UserMessenger is an error with an associated user-facing message

                        Source Files