Skip to content
Myr

docs: document panic conditions in Handle, StaticFS, and Bind (#4797)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Handle panics on invalid HTTP method, StaticFS panics when relativePath contains URL parameters, and Bind panics when given a pointer. These panic conditions are reasonable startup-time guards but were not documented, making the crash risk undiscoverable without reading source. Fixes #4594

authored by Ruben Pari · committed by GitHub · parent 2a1a14f · Browse files · 2 files+4 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+4 added−0 removed

  • other:+4 added−0 removed

2 files+4 added−0 removed

modified:routergroup.go+3 added
98 unmodified lines
99//100// This function is intended for bulk loading and to allow the usage of less101// frequently used, non-standardized or custom methods (e.g. for internal102// communication with a proxy).103//104// It panics if httpMethod is not a valid HTTP method (uppercase ASCII letters only).105func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes {106 if matched := regEnLetter.MatchString(httpMethod); !matched {107 panic("http method " + httpMethod + " is not valid")108 }
99 unmodified lines
208}209210// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.211// Gin by default uses: gin.Dir()212// It panics if relativePath contains URL parameters (: or *).213func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {214 if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {215 panic("URL parameters can not be used when serving a static folder")216 }
53 unmodified lines
modified:utils.go+1 added
24 unmodified lines
25// localhostIPv6 indicates the default localhost IPv6 address.26const localhostIPv6 = "::1"2728// Bind is a helper function for given interface object and returns a Gin middleware.29// It panics if val is a pointer; pass the struct value directly (e.g., gin.Bind(Struct{}) not gin.Bind(&Struct{})).30func Bind(val any) HandlerFunc {31 value := reflect.ValueOf(val)32 if value.Kind() == reflect.Ptr {33 panic(`Bind struct can not be a pointer. Example:
155 unmodified lines