jsonutil

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2017 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jsonutil provides support functions for dealing with json responses and specificially focuses on handling errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RespondJSONError

func RespondJSONError(w http.ResponseWriter, jre *JSONResponseError) (int, error)

RespondJSONError will bootstrap and write out a valid json repsonse using the provided JSONResponseError. It returns the results of fmt.Printf against the provided http.ResponseWriter, which is expected to be the number of bytes written and any write error encountered.

Types

type JSONResponseError

type JSONResponseError struct {
	Output string
	// contains filtered or unexported fields
}

JSONResponseError implements a JSON specific ResponseErrorBag.

func NewJSONResponseError

func NewJSONResponseError(status int, messages ...string) *JSONResponseError

NewJSONResponseError creates a new instance of a JSONResponseError. While a status is required, error messages are optionally added in a shorthand format if desired.

NewJSONResponseError(500)                   // new empty JSONResponseError
NewJSONResponseError(500, "internal error") // new JSONResponseError with single error

func (*JSONResponseError) AddError

func (jre *JSONResponseError) AddError(details string, status ...int) *JSONResponseError

AddError adds a new ResponseError, while accepting an optional status code specific to the new ResponseError.

func (*JSONResponseError) Errors

func (jre *JSONResponseError) Errors() []ResponseError

Errors acts as a getter to provide a slice of all of the ResponseErrors.

func (*JSONResponseError) Render

func (jre *JSONResponseError) Render() string

Render marshals the JSONResponseError to a valid JSON string and sets the Output field to the rendered output. This method does not simply marshal the instance, but also formats the output to a usable format for responses.

func (*JSONResponseError) Status

func (jre *JSONResponseError) Status() int

Status acts as a getter to provide the high-level status.

type ResponseError

type ResponseError struct {
	Details string `json:"details"`
	Status  int    `json:"status,string"`
}

ResponseError holds details and a status code related to a specific error.

type ResponseErrorBag

type ResponseErrorBag interface {
	// AddError will create and add a new ResponseError into this ResponseErrorBag
	// instance. It also accepts an optional status code specific to the error, or
	// defaults to the ResponseErrorBag's high-level status code.
	AddError(details string, status ...int) *ResponseErrorBag
	Errors() []ResponseError
	Render() string
	Status() int
}

ResponseErrorBag handles the management and usage of ResponseErrors. It is also responsible for maintaining a high-level status code, which expected to be reflected in the response header.

Jump to

Keyboard shortcuts

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