arpc

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 6 Imported by: 1

README

arpc

ARPC is the Acoshift's opinionated HTTP-RPC styled api

HTTP Status Code

ARPC will response http with only these 3 status codes

  • 200 OK - function works as expected
  • 400 Bad Request - developer (api caller) error, should never happened in production
  • 500 Internal Server Error - server error, should never happened (server broken)

Example Responses

Success Result
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
	"ok": true,
	"result": {
		// result object
	}
}
Error Result
  • Validate error
  • Precondition failed
  • User error
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "some error message"
	}
}
Method not allowed
  • Developer (api caller) send invalid method
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "method not allowed"
	}
}
Function not found
  • Developer (api caller) call not exists function
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "not found"
	}
}
Unsupported Content-Type
  • Developer (api caller) send invalid content type
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {
		"message": "unsupported content type"
	}
}
Internal Server Error
  • Server broken !!!
HTTP/1.1 500 Internal Server Error
Content-Type: application/json; charset=utf-8

{
	"ok": false,
	"error": {} // internal error always return empty object
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupported = NewProtocolError("unsupported content type")
)

predefined errors

Functions

func Handler

func Handler(f interface{}) http.Handler

Handler converts f to handler

func NewError

func NewError(message string) error

NewError creates new Error

func NewProtocolError

func NewProtocolError(message string) error

func NotFound

func NotFound(w http.ResponseWriter, r *http.Request)

func NotFoundHandler

func NotFoundHandler() http.Handler

func SetValidate added in v1.1.0

func SetValidate(enable bool)

SetValidate sets hrpc manager validate state

func WrapError

func WrapError(err error) error

WrapError wraps given error with Error

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error always return 200 status with false ok value use this error for validate, precondition failed, etc.

func (*Error) Error

func (err *Error) Error() string

Error implements error

func (*Error) MarshalJSON added in v1.1.0

func (err *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (*Error) OKError added in v1.1.0

func (err *Error) OKError()

OKError implements OKError

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap implements errors.Unwarp

type FormUnmarshaler

type FormUnmarshaler interface {
	UnmarshalForm(v url.Values) error
}

FormUnmarshaler interface

type MultipartFormUnmarshaler

type MultipartFormUnmarshaler interface {
	UnmarshalMultipartForm(v *multipart.Form) error
}

MultipartFormUnmarshaler interface

type OKError added in v1.1.0

type OKError interface {
	OKError()
}

OKError implements this interface to mark errors as 200

type ProtocolError

type ProtocolError struct {
	Message string `json:"message"`
}

ProtocolError always returns 400 status with false ok value only use this error for invalid protocol usages

func (*ProtocolError) Error

func (err *ProtocolError) Error() string

type RequestAdapter

type RequestAdapter interface {
	AdaptRequest(r *http.Request)
}

RequestAdapter converts request to arpc before decode

type RequestUnmarshaler

type RequestUnmarshaler interface {
	UnmarshalRequest(r *http.Request) error
}

RequestUnmarshaler interface

Jump to

Keyboard shortcuts

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