response

package
v0.0.0-...-31b340c Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 5 Imported by: 0

README

validation with translation handlers for gin:

func (h handler) handler(ctx gin.Context) {

	var request struct {
		Name string `json:"name" binding:"required"`
		Age  int    `json:"age" binding:"required"`
	}
	
	if err := ctx.ShouldBindJSON(&request); err != nil {
		
		// h.translation is a instance of the translation pkg
		// ctx is a instance of the gin.Context
		// err is a instance of the gin binding error
		// http.StatusUnprocessableEntity is a http status code
		response.NewResponse(h.translation).
			Validation(err).
			WithStatusCode(http.StatusUnprocessableEntity)
			Echo(ctx)
		
	    return 
	}
}

for the response has payload you can use the following code:

func (h handler) handler(ctx gin.Context) {
    // payload is a instance of the struct that you want to return
    // http.StatusOK is a http status code
    response.NewResponse(h.translation).
        Payload(payload).
		WithStatusCode(http.StatusOK)
        Echo(ctx)
}

for the response has error you can use the following code:

func (h handler) handler(ctx gin.Context) {
	
    // err is a instance of the response.Error
    // http.StatusForbidden is a http status code
    response.NewResponse(h.translation).
        WithError(err).
        WithStatusCode(http.StatusForbidden).
        Echo()
	
    // if you need mapping status code to the error override the StatusCodeMapping
    response.NewResponse(h.translation, StatusCodeMapping).
        WithError(err).
		Echo(ctx)
	
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error interface {
	Error() string
	GetType() string
	GetAttributes() map[string]interface{}
}

type ErrorResponse

type ErrorResponse struct {
	TypeInfo   string                 `json:"type_info"`
	Status     int                    `json:"status"`
	Detail     string                 `json:"detail"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
}

type Resource

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

func (*Resource) Echo

func (r *Resource) Echo(ctx *gin.Context)

Echo sends the response to the client.

func (*Resource) EchoPure

func (r *Resource) EchoPure() (statusCode int, response map[string]any)

EchoPure returns the response to be sent to the client.

func (*Resource) Validation

func (r *Resource) Validation(err error) Response

Validation sets the validation error to be sent to the client.

func (*Resource) WithError

func (r *Resource) WithError(err error) Response

WithError sets the error to be sent to the client.

func (*Resource) WithMessage

func (r *Resource) WithMessage(message string, args ...map[string]interface{}) Response

WithMessage sets the message to be sent to the client.

func (*Resource) WithMeta

func (r *Resource) WithMeta(data interface{}) Response

WithMeta sets the meta data to be sent to the client.

func (*Resource) WithPayload

func (r *Resource) WithPayload(data any) Response

WithPayload sets the data to be sent to the client.

func (*Resource) WithStatusCode

func (r *Resource) WithStatusCode(statusCode int) Response

WithStatusCode sets the status code to be sent to the client.

type Response

type Response interface {
	Validation(err error) Response
	WithPayload(data any) Response
	WithMessage(message string, args ...map[string]interface{}) Response
	WithError(err error) Response
	WithMeta(data interface{}) Response
	Echo(ctx *gin.Context)
	EchoPure() (statusCode int, response map[string]any)
	WithStatusCode(statusCode int) Response
}

func NewResponse

func NewResponse(
	trans translation.Translation,
	statusCodeMappings ...map[string]int,
) Response

NewResponse creates a new response.

type ServiceError

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

func NewServiceError

func NewServiceError(
	err error,
	attrs ...map[string]interface{},
) *ServiceError

func (*ServiceError) Error

func (e *ServiceError) Error() string

func (*ServiceError) GetAttributes

func (e *ServiceError) GetAttributes() map[string]interface{}

func (*ServiceError) GetMessage

func (e *ServiceError) GetMessage() string

func (*ServiceError) GetType

func (e *ServiceError) GetType() string

func (*ServiceError) SetType

func (e *ServiceError) SetType(errorType string) Error

type ValidationError

type ValidationError struct {
	Property string `json:"property"`
	Message  string `json:"message"`
}

type Validations

type Validations map[string][]string

Jump to

Keyboard shortcuts

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