restful

package module
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2022 License: MIT Imports: 9 Imported by: 0

README

go-restful

This is a basic toolset to simplify the work with RESTful errors throughout an application.

Disclaimer

This project is in production use in some of my larger projects. In our experience, the system is very stable.

Documentation

Index

Constants

View Source
const (
	LimitDefault = 50
)

Variables

View Source
var (
	Development bool

	MsgServerError  = "server-error"
	MsgUnauthorized = "not-authenticated"
	MsgForbidden    = "access-denied"
	MsgNotFound     = "not-found"
)
View Source
var (
	ErrNoFields              = errors.New("No fields selected")
	ErrFilterStructure       = errors.New("the filter string does not match the allowed structure")
	ErrFilterNotAllowed      = errors.New("the filter is not allowed")
	ErrOrderInvalidStructure = errors.New("The order string does not match the allowed structure")
	ErrOrderNotAllowed       = errors.New("The order is not allowed")
)

Functions

func Count added in v1.0.2

func Count(cfg Config, req Request) (query string, args map[string]interface{}, err error)

func Field

func Field(name string) field

func Prepare

func Prepare(cfg Config, req Request) (query string, args map[string]interface{}, err error)

func PrepareCount

func PrepareCount(cfg Config, req Request, searchTarget string) (query string, args map[string]interface{}, err error)

Types

type Config

type Config struct {
	Fields           Fields
	Distinct         bool
	Table            string
	Where            string
	GroupBy          string
	CalcRows         bool
	AdditionalParams Params
}

The query builder configuration structure.

type Fields

type Fields []field

type M

type M map[string]interface{}

M is a simple string map for result parameters

type OrderType

type OrderType int
const (
	OrderNone OrderType = iota
	DESC      OrderType = iota
	ASC       OrderType = iota
)

type Params

type Params map[string]interface{}

Additional params that will be injected into the overall query building proces.

type Request

type Request struct {
	Fields string `json:"fields" form:"fields" query:"fields"`
	Filter string `json:"filter" form:"filter" query:"filter"`
	Order  string `json:"order" form:"order" query:"order"`
	Limit  uint   `json:"limit" form:"limit" query:"limit"`
	Offset uint   `json:"offset" form:"offset" query:"offset"`
	Search string `json:"search" form:"search" query:"search"`
}

Structure that can be used in conjunction with request handling to simplify the config collection process.

type Response

type Response interface {
	error

	// GetCode returns the code of this response
	GetCode() int

	// SetCode sets the response code
	SetCode(int)

	// GetTracking returns the tracking id
	GetTracking() string

	// GetReason set the reason for this
	GetReason() string

	// GetMessage returns the core message of this response
	GetMessage() string

	// SetMessage sets the core message
	SetMessage(string)

	// GetStack returns the custom error stack
	GetStack() []string

	// GetSource returns the original error
	GetSource() error
	// contains filtered or unexported methods
}

Response

func BadRequest

func BadRequest(msg string, info ...interface{}) Response

func BadRequestWithReason

func BadRequestWithReason(msg string, reason string, info ...interface{}) Response

func Forbidden

func Forbidden(info ...interface{}) Response

func ForbiddenWithReason

func ForbiddenWithReason(reason string, info ...interface{}) Response

func InvalidForm

func InvalidForm(err error, info ...interface{}) Response

func InvalidJSON

func InvalidJSON(err error, info ...interface{}) Response

func NotFound

func NotFound(info ...interface{}) Response

func NotFoundWithReason

func NotFoundWithReason(reason string, info ...interface{}) Response

func ServerError

func ServerError(err error, info ...interface{}) Response

func ServerErrorWithReason

func ServerErrorWithReason(err error, reason string, info ...interface{}) Response

func Stack

func Stack(err error, info ...interface{}) Response

func Stackf

func Stackf(err error, info ...interface{}) Response

func Unauthorized

func Unauthorized(info ...interface{}) Response

func UnauthorizedWithReason

func UnauthorizedWithReason(reason string, info ...interface{}) Response

Jump to

Keyboard shortcuts

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