respond

package
v0.2.19 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorGRPC

func ErrorGRPC[T any](ctx context.Context, err error, options ...Option) (T, error)

ErrorGRPC tries to map err to errkit.Error and based on result writes standard gRPC error with status statusCode to the response writer.

func ErrorHTTP

func ErrorHTTP(w http.ResponseWriter, r *http.Request, err error, options ...Option)

ErrorHTTP tries to map err to errkit.Error and based on result writes standard HTTP error with status statusCode to the response writer.

func HTML

func HTML(w http.ResponseWriter, r *http.Request, v []byte, options ...Option)

HTML tries to encode v into json representation and write it to response writer.

func JSON

func JSON(w http.ResponseWriter, r *http.Request, v any, options ...Option)

JSON tries to encode v into json representation and write it to response writer.

func SetGRPCErrorResponder

func SetGRPCErrorResponder(responder GRPCErrorResponder)

SetGRPCErrorResponder sets the given responder as errGRPCResponder.

func SetHTMLTemplateProvider

func SetHTMLTemplateProvider(templater HTMLTemplateProvider)

SetHTMLTemplateProvider sets the given htmlTemplater as htmlTemplater.

func SetHTTPErrorResponder

func SetHTTPErrorResponder(responder HTTPErrorResponder)

SetHTTPErrorResponder sets the given responder as errHTTPResponder.

func Status

func Status(w http.ResponseWriter, _ *http.Request, statusCode int, options ...Option)

Status writes an HTTP status to the w http.ResponseWriter.

func TEXT

func TEXT(w http.ResponseWriter, r *http.Request, v []byte, options ...Option)

TEXT tries to write v to response writer.

func TemplateHTML

func TemplateHTML(w http.ResponseWriter, r *http.Request, name string, v any, options ...Option)

TemplateHTML generates an HTML template response for the given name and data. It sets the Content-Type header to "text/html; charset=utf-8" and writes the response with the specified status code. If the template execution fails, an error will be logged and a 500 Internal Server Error response will be sent. Additional options can be passed to modify the response using the Option functions.

Types

type GRPCErrorResponder

type GRPCErrorResponder func(err error) error

GRPCErrorResponder represents a function type that handles and responds to GRPC errors.

type HTMLTemplateProvider

type HTMLTemplateProvider interface {
	// Template renders the HTML templates by given name.
	Template(ctx context.Context, name string) (*template.Template, error)
}

HTMLTemplateProvider wraps a Template method to render requested HTML template.

type HTTPErrorResponder

type HTTPErrorResponder func(w http.ResponseWriter, err error)

HTTPErrorResponder represents a function which should be called to respond with an error on HTTP call.

type Option

type Option func(o *ResponseOptions)

Option represents a function type that modifies ResponseOptions for an HTTP response.

func WithErrorReport added in v0.2.19

func WithErrorReport() Option

WithErrorReport is an Option function that will enable error reporting by the errkit.ErrorReporter. Used in

func WithHeader

func WithHeader(key, value string) Option

WithHeader is an Option function that adds the given key-value pair to the headers of the ResponseOptions. The headers are used to modify the headers of an HTTP response.

func WithStatus

func WithStatus(code int) Option

WithStatus sets the given status code as the statusCode field of the ResponseOptions parameter.

type ResponseOptions

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

ResponseOptions represents the options for an HTTP response.

func Options

func Options(w http.ResponseWriter, options ...Option) *ResponseOptions

Options returns a pointer to a new ResponseOptions object with default values and applies the given options to it.

Jump to

Keyboard shortcuts

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