jsonutil

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 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 GenericSelfRespondingJSONError added in v0.1.3

type GenericSelfRespondingJSONError struct {
	// Details is a human readable message that describes the error.
	Details string `json:"details,omitempty"`
	// Status is appropriate the HTTP status code for the error.
	Status int `json:"status,string"`
}

GenericSelfRespondingJSONError is a generic implementation of of the SelfRespondingJSONError interface. This is good to use when there is little to no logic in defining error messages or status codes.

func (*GenericSelfRespondingJSONError) Render added in v0.1.3

Render writes the contents of the GenericSelfRespondingJSONError to the provided http.ResponseWriter.

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.

type SelfRespondingJSONError added in v0.1.3

type SelfRespondingJSONError interface {
	// Render will write the contents of the JSON error to the provided
	// http.ResponseWriter
	Render(w http.ResponseWriter)
}

SelfRespondingJSONError is an interfaces representing the ability to render its own contents.

func DatabaseConnectError added in v0.1.3

func DatabaseConnectError() SelfRespondingJSONError

DatabaseConnectError is a wrapper for a new GenericSelfRespondingJSONError that will provide a predefined message for a database connection error. It returns a SelfRespondingJSONError, in the form of a GenericSelfRespondingJSONError, so that rendering can be chained.

Example:

DatabaseConnectError().Render(w)

func KubeClientConnectionError added in v0.1.3

func KubeClientConnectionError() SelfRespondingJSONError

KubeClientConnectionError provides a predefined message for a Kubernetes client connection error. Rendering can be chained.

Jump to

Keyboard shortcuts

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