#3429·fiber

[问题]:自定义类型查询解析器中缺少错误详细信息

作者: msaboor35创建于 2025年4月29日更新于 2026年2月7日
标签🤔 Question🛠 In Progress

// 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,
            }},