fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🤗 [Question]: Missing Error Details in Query Parser for Custom Types

Open msaboor35 opened this issue 8 months ago • 4 comments

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’ve 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:

  • [x] I agree to follow Fiber's Code of Conduct.
  • [x] I have checked for existing issues that describe my questions prior to opening this one.
  • [x] I understand that improperly formatted questions may be closed without explanation.

msaboor35 avatar Apr 29 '25 22:04 msaboor35

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

welcome[bot] avatar Apr 29 '25 22:04 welcome[bot]

@gaby hi, I would like to work on this issue if possible...

christinabrgs avatar Oct 13 '25 01:10 christinabrgs

@christinabrgs Sure, make sure to use main which is the next major release of Fiber v3. First thing would be checking if this is also missing in v3.

gaby avatar Oct 13 '25 12:10 gaby

The same issue exists in v3 as well. SetParserDecoder and its related machinery was moved binder package in v3 from the main fiber package. The following modified main function demonstrates the same issue in v3.

package main

// just showing new imports
import (
	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/binder"
)

// only main function is changed slightly, rest remains same.
func main() {
	app := fiber.New()

	binder.SetParserDecoder(binder.ParserConfig{
		ParserType: []binder.ParserType{{
			CustomType: CustomField(0),
			Converter:  ParseCustomField,
		}},
	})

	app.Get("/", func(c fiber.Ctx) error {
		var q Query
		if err := c.Bind().Query(&q); err != nil {
			return fiber.NewError(fiber.StatusBadRequest, err.Error())
		}
		return c.JSON(q)
	})

	log.Fatal(app.Listen(":3000"))
}

msaboor35 avatar Oct 15 '25 16:10 msaboor35