Global Options
Overview
Section titled “Overview”vel provides a global configuration system through the GlobalOpts variable,
allowing you to customize framework behavior across all handlers.
GlobalOpts Structure
Section titled “GlobalOpts Structure”The GlobalOpts variable is of type Opts and provides the following configuration options:
type Opts struct { ProcessErr func(r *http.Request, e *Error) MapCodeToStatus func(code string) int SkipOptionMethod bool}
var GlobalOpts = Opts{ ProcessErr: nil, MapCodeToStatus: func(code string) int { if code == "" { return http.StatusInternalServerError } return http.StatusBadRequest }, SkipOptionMethod: false,}Configuration Options
Section titled “Configuration Options”- ProcessErr - Custom error processing function called before errors are returned to clients
- MapCodeToStatus - Function that maps error codes to HTTP status codes
- SkipOptionMethod - Boolean flag to control automatic OPTIONS method handling
Default Behavior
Section titled “Default Behavior”- ProcessErr:
nil(no custom error processing) - MapCodeToStatus: Returns 500 for empty error codes, 400 for all others
- SkipOptionMethod:
false(automatic OPTIONS handling enabled)
Custom Error Processing
Section titled “Custom Error Processing”Configure global error processing with the ProcessErr function to implement logging, metrics, and custom error handling.
Basic Error Processing
Section titled “Basic Error Processing”vel.GlobalOpts.ProcessErr = func(r *http.Request, e *vel.Error) { // Log errors with request context log.Printf("API Error: %s - %s [%s %s]", e.Code, e.Message, r.Method, r.URL.Path)}HTTP Status Code Mapping
Section titled “HTTP Status Code Mapping”Configure how error codes map to HTTP status codes using the MapCodeToStatus function.
Default Status Code Mapping
Section titled “Default Status Code Mapping”// Default implementationvel.GlobalOpts.MapCodeToStatus = func(code string) int { if code == "" { return http.StatusInternalServerError // 500 } return http.StatusBadRequest // 400}OPTIONS Method Handling
Section titled “OPTIONS Method Handling”Control automatic OPTIONS method registration with the SkipOptionMethod option.
Default OPTIONS Handling
Section titled “Default OPTIONS Handling”By default, vel automatically registers OPTIONS handlers for all registered routes:
// Default behavior - OPTIONS handlers are automatically createdvel.GlobalOpts.SkipOptionMethod = false
// Register a POST handlervel.RegisterPost(router, "users", CreateUserHandler)
// vel automatically registers:// OPTIONS /users -> returns 200 OKDisabling Automatic OPTIONS
Section titled “Disabling Automatic OPTIONS”// Disable automatic OPTIONS handlingvel.GlobalOpts.SkipOptionMethod = true