common

package
v0.0.0-...-4faa3c3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2017 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// CodeInvalidParam represents a missing parameter
	CodeInvalidParam = "invalid_parameter"

	// CodeServerError represents a server error
	CodeServerError = "server_error"

	// CodeAuthorizationError represents an authorization error
	CodeAuthorizationError = "auth_error"

	// CodePutError represents a failure to put an object
	CodePutError = "put_error"
)
View Source
var (
	// ServerError describe a server error
	ServerError = NewSingleAPIErr(500, "", "server_error", "server error", nil)
	// BodyMalformedError describes an error about a malformed json body
	BodyMalformedError = NewSingleAPIErr(400, "", "invalid_body", "malformed json body", nil)
)

Functions

func EasyHandle

func EasyHandle(method string, handler func(w http.ResponseWriter, r *http.Request) (interface{}, int)) func(http.ResponseWriter, *http.Request)

EasyHandle takes a custom handle that returns any value type and a status code. An error response value whose message is a JSON string is considered to be a decoded common.APIError and will be encoded back to APIError and returned as JSON. The status code provided in APIError is used as the response status code. Other errors are returned as text. None errors are coerced and returned as JSON.

func GetKeyByValue

func GetKeyByValue(mapping map[string]string, value, def string) string

GetKeyByValue gets a key by its value. The first key with a matching value is returned. If value is not found, def is returned

func MapFields

func MapFields(mapping map[string]string, unmappedObj interface{}) error

MapFields applies a mapping to a unmapped object

func MultiObjectResp

func MultiObjectResp(objectType string, attrs []map[string]interface{}) map[string]interface{}

MultiObjectResp creates standard response format for multiple objects

func NewAPIError

func NewAPIError(status int, msg string, errors Errors) error

NewAPIError creates an APIError that is encoded to JSON and returned as a standard error.

func NewMultiAPIErr

func NewMultiAPIErr(status int, message string, errs []Error) error

NewMultiAPIErr returns a response with multiple errors

func NewSingleAPIErr

func NewSingleAPIErr(status int, code, field, message string, links map[string]string) error

NewSingleAPIErr returns a response with a single error

func SingleObjectResp

func SingleObjectResp(objectType string, attr map[string]interface{}) map[string]interface{}

SingleObjectResp creates standard response format for only a single object

func UnMapFields

func UnMapFields(mapping map[string]string, mappedObj interface{}) error

UnMapFields takes a mapping, an object or slice of objects that have mapped fields and produces a new object or slice of objects where the mapped, custom fields are converted to the original columns

Types

type APIError

type APIError struct {
	Message    string
	Errors     Errors
	StatusCode int
}

APIError describes an error from a server endpoint

func (*APIError) Error

func (e *APIError) Error() string

type Error

type Error struct {
	Status  string            `json:"status,omitempty" structs:"status,omitempty" mapstructure:"status,omitempty"`
	Code    string            `json:"code,omitempty" structs:"code,omitempty" mapstructure:"code,omitempty"`
	Field   string            `json:"field,omitempty" structs:"field,omitempty" mapstructure:"field,omitempty"`
	Message string            `json:"message,omitempty" structs:"message,omitempty" mapstructure:"message,omitempty"`
	Links   map[string]string `json:"links,omitempty" structs:"links,omitempty" mapstructure:"links,omitempty"`
}

Error defines a single error

type Errors

type Errors struct {
	Errors []Error `json:"errors,omitempty" structs:"errors,omitempty" mapstructure:"errors,omitempty"`
}

Errors defines a collection of errors

Jump to

Keyboard shortcuts

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