Skip to content

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

Open
@msaboor35

Description

@msaboor35

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:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions