mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
add new errors.Intercept package-level function
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
@@ -16,10 +17,10 @@ func main() {
|
||||
// Create a new service and pass it to the handlers.
|
||||
service := new(myService)
|
||||
|
||||
app.Post("/", createHandler(service)) // OR: errors.CreateHandler(service.Create)
|
||||
app.Get("/", listAllHandler(service)) // OR errors.Handler(service.ListAll, errors.Value(ListRequest{}))
|
||||
app.Post("/page", listHandler(service)) // OR: errors.ListHandler(service.ListPaginated)
|
||||
app.Delete("/{id:string}", deleteHandler(service)) // OR: errors.NoContentOrNotModifiedHandler(service.DeleteWithFeedback, errors.PathParam[string]("id"))
|
||||
app.Post("/", errors.Intercept(afterServiceCallButBeforeDataSent), createHandler(service)) // OR: errors.CreateHandler(service.Create)
|
||||
app.Get("/", listAllHandler(service)) // OR errors.Handler(service.ListAll, errors.Value(ListRequest{}))
|
||||
app.Post("/page", listHandler(service)) // OR: errors.ListHandler(service.ListPaginated)
|
||||
app.Delete("/{id:string}", deleteHandler(service)) // OR: errors.NoContentOrNotModifiedHandler(service.DeleteWithFeedback, errors.PathParam[string]("id"))
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
@@ -95,7 +96,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// ValidateContext implements the errors.ContextValidator interface.
|
||||
// HandleRequest implements the errors.RequestHandler interface.
|
||||
// It validates the request body and returns an error if the request body is invalid.
|
||||
// You can also alter the "r" CreateRequest before calling the service method,
|
||||
// e.g. give a default value to a field if it's empty or set an ID based on a path parameter.
|
||||
@@ -111,7 +112,7 @@ type (
|
||||
// validation.Slice("hobbies", r.Hobbies).Length(1, 10),
|
||||
// )
|
||||
// }
|
||||
func (r *CreateRequest) ValidateContext(ctx iris.Context) error {
|
||||
func (r *CreateRequest) HandleRequest(ctx iris.Context) error {
|
||||
// To pass custom validation functions:
|
||||
// return validation.Join(
|
||||
// validation.String("fullname", r.Fullname).Func(customStringFuncHere),
|
||||
@@ -152,6 +153,20 @@ func (r *CreateRequest) ValidateContext(ctx iris.Context) error {
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
// HandleResponse implements the errors.ResponseHandler interface.
|
||||
func (r *CreateRequest) HandleResponse(ctx iris.Context, resp *CreateResponse) error {
|
||||
fmt.Printf("request got: %+v\nresponse sent: %#+v\n", r, resp)
|
||||
|
||||
return nil // fmt.Errorf("let's fire an internal server error just for the shake of the example") // return nil to continue.
|
||||
}
|
||||
*/
|
||||
|
||||
func afterServiceCallButBeforeDataSent(ctx iris.Context, req *CreateRequest, resp *CreateResponse) error {
|
||||
fmt.Printf("intercept: request got: %+v\nresponse sent: %#+v\n", req, resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *myService) Create(ctx context.Context, in CreateRequest) (CreateResponse, error) {
|
||||
arr := strings.Split(in.Fullname, " ")
|
||||
firstname, lastname := arr[0], arr[1]
|
||||
|
||||
Reference in New Issue
Block a user