Question Description
When using QueryParser, we can register a custom parser for decoding the query parameters with fiber.SetParserDecoder. The type of ParserType.Converter in ParserConfig is func(val string) reflect.Value. This function does not return an error.
In the examples I鈥檝e seen, the usual approach is to return a zero value in case of an error. The schema.Decoder's decode method checks if the converter returns a zero value using value.IsValid(). If it returns an zero value, it returns a ConversionError. However, the Err field in the ConversionError is not populated because the Converter function does not return an error.
My goal is to receive error details when performing conversions, similar to how errors are handled when using TextUnmarshaler with built-in types.
Example of error from ctx.QueryParser which uses TextUnmarshaler
failed to decode: schema: error converting value for "textField". Details: TextField: parseInt: strconv.Atoi: parsing "a": invalid syntax
Example of error from ctx.QueryParser which uses my custom type registered using SetParserDecoder
failed to decode: schema: error converting value for "customField"
Environment:
Fiber Version: v2.52.6
Go Version: go1.24.2
OS: linux/amd64
Code Snippet (optional)
package main
import (
"errors"
"fmt"
"log"
"reflect"
"strconv"
"github.com/gofiber/fiber/v2"
)
// Query struct for testing
type Query struct {
TextField TextField `query:"textField"` // Built-in type: error includes "Details"
CustomField CustomField `query:"customField"` // Custom type: error does NOT include "Details"
}
// parseInt function to handle common integer parsing logic
func parseInt(s string) (int, error) {
n, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("parseInt: %w", err)
}
if n < 0 {
return 0, errors.New("parseInt: must be non-negative")
}
return n, nil
}
// TextField implementing TextUnmarshaler
type TextField int
func (t *TextField) UnmarshalText(b []byte) error {
if n, err := parseInt(string(b)); err != nil {
return fmt.Errorf("TextField: %w", err)
} else {
*t = TextField(n)
return nil
}
}
// CustomField with custom parser
type CustomField int
func ParseCustomField(val string) reflect.Value {
if n, err := parseInt(val); err != nil {
return reflect.Value{}
} else {
c := CustomField(n)
return reflect.ValueOf(c)
}
}
func main() {
app := fiber.New()
// Register custom parser for CustomField
fiber.SetParserDecoder(fiber.ParserConfig{
ParserType: []fiber.ParserType{{
Customtype: CustomField(0),
Converter: ParseCustomField,
}},
})
app.Get("/", func(c *fiber.Ctx) error {
var q Query
if err := c.QueryParser(&q); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(q)
})
log.Fatal(app.Listen(":3000"))
}
Checklist:
Question Description
When using
QueryParser, we can register a custom parser for decoding the query parameters withfiber.SetParserDecoder. The type ofParserType.ConverterinParserConfigisfunc(val string) reflect.Value. This function does not return an error.In the examples I鈥檝e seen, the usual approach is to return a zero value in case of an error. The
schema.Decoder'sdecodemethod checks if the converter returns a zero value usingvalue.IsValid(). If it returns an zero value, it returns aConversionError. However, theErrfield in theConversionErroris not populated because theConverterfunction does not return an error.My goal is to receive error details when performing conversions, similar to how errors are handled when using
TextUnmarshalerwith built-in types.Example of error from
ctx.QueryParserwhich usesTextUnmarshalerfailed to decode: schema: error converting value for "textField". Details: TextField: parseInt: strconv.Atoi: parsing "a": invalid syntaxExample of error from
ctx.QueryParserwhich uses my custom type registered usingSetParserDecoderfailed to decode: schema: error converting value for "customField"Environment:
Fiber Version: v2.52.6
Go Version: go1.24.2
OS: linux/amd64
Code Snippet (optional)
Checklist: