MCPcopy Index your code
hub / github.com/labstack/echo / ExampleValueBinder_BindError

Function ExampleValueBinder_BindError

binder_external_test.go:55–89  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

53}
54
55func ExampleValueBinder_BindError() {
56 // example route function that binds query params to different destinations and stops binding on first bind error
57 failFastRouteFunc := func(c *echo.Context) error {
58 var opts struct {
59 IDs []int64
60 Active bool
61 }
62 length := int64(50) // default length is 50
63
64 // create binder that stops binding at first error
65 b := echo.QueryParamsBinder(c)
66
67 err := b.Int64("length", &length).
68 Int64s("ids", &opts.IDs).
69 Bool("active", &opts.Active).
70 BindError() // returns first binding error
71 if err != nil {
72 bErr := err.(*echo.BindingError)
73 return fmt.Errorf("my own custom error for field: %s values: %v", bErr.Field, bErr.Values)
74 }
75 fmt.Printf("active = %v, length = %v, ids = %v\n", opts.Active, length, opts.IDs)
76
77 return c.JSON(http.StatusOK, opts)
78 }
79
80 e := echo.New()
81 c := e.NewContext(
82 httptest.NewRequest(http.MethodGet, "/api/endpoint?active=true&length=25&ids=1&ids=2&ids=3", nil),
83 httptest.NewRecorder(),
84 )
85
86 _ = failFastRouteFunc(c)
87
88 // Output: active = true, length = 25, ids = [1 2 3]
89}
90
91func ExampleValueBinder_CustomFunc() {
92 // example route function that binds query params using custom function closure

Callers

nothing calls this directly

Calls 6

JSONMethod · 0.95
BindErrorMethod · 0.80
BoolMethod · 0.80
Int64sMethod · 0.80
Int64Method · 0.80
NewContextMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…