{"record":{"id":"743489c219cb51b6","repo":"gofiber/fiber","slug":"use-invalid-handler-v","errorCode":null,"errorMessage":"use: invalid handler %v\n","messagePattern":"use: invalid handler (.+?)\n","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"app.go","lineNumber":1046,"sourceCode":"// This method will match all HTTP verbs: GET, POST, PUT, HEAD etc...\nfunc (app *App) Use(args ...any) Router {\n\tvar prefix string\n\tvar subApp *App\n\tvar prefixes []string\n\tvar handlers []Handler\n\n\tfor i := range args {\n\t\tswitch arg := args[i].(type) {\n\t\tcase string:\n\t\t\tprefix = arg\n\t\tcase *App:\n\t\t\tsubApp = arg\n\t\tcase []string:\n\t\t\tprefixes = arg\n\t\tdefault:\n\t\t\thandler, ok := toFiberHandler(arg)\n\t\t\tif !ok {\n\t\t\t\tpanic(fmt.Sprintf(\"use: invalid handler %v\\n\", reflect.TypeOf(arg)))\n\t\t\t}\n\t\t\thandlers = append(handlers, handler)\n\t\t}\n\t}\n\n\tif len(prefixes) == 0 {\n\t\tprefixes = append(prefixes, prefix)\n\t}\n\n\tfor _, prefix := range prefixes {\n\t\tif subApp != nil {\n\t\t\treturn app.mount(prefix, subApp)\n\t\t}\n\n\t\tapp.register([]string{methodUse}, prefix, nil, handlers...)\n\t}\n\n\treturn app","sourceCodeStart":1028,"sourceCodeEnd":1064,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/app.go#L1028-L1064","documentation":"Inside App.Use, arguments are dispatched by type: string becomes a prefix, *App becomes a sub-app to mount, []string becomes multiple prefixes, and anything else is funneled through toFiberHandler. If none match, the app panics showing the Go type of the offending argument. This is a route-registration-time programming error.","triggerScenarios":"Calling app.Use(someInt), app.Use(myStruct), or app.Use(aFuncWithWrongSignature). The message uses reflect.TypeOf(arg) so you can see exactly what type was rejected.","commonSituations":"Passing a *Router or *Group where *App or a Handler was expected; passing a config struct; passing a handler chain built by a helper that returned the wrong type; refactor that changed Use calls.","solutions":["Check the panic output for the rejected type and locate that argument in your app.Use(...) call.","Convert the argument to func(*fiber.Ctx) error or to a *fiber.App (for mounting) before passing.","For mounting a sub-app, pass *fiber.App explicitly: app.Use(\"/sub\", subApp).","For a Group, use group.Use(...) on the group, not app.Use(group)."],"exampleFix":"// before\nsubApp := fiber.New()\napp.Use(subApp.Group(\"/api\")) // Group is not accepted here\n\n// after\napi := subApp.Group(\"/api\")\napp.Use(\"/api\", subApp)","handlingStrategy":"type-guard","validationCode":"// Classify Use arguments before calling app.Use\nfunc classifyUseArg(arg any) (prefix string, sub *fiber.App, h fiber.Handler, err error) {\n    switch v := arg.(type) {\n    case string: return v, nil, nil, nil\n    case *fiber.App: return \"\", v, nil, nil\n    case fiber.Handler, func(*fiber.Ctx) error:\n        return \"\", nil, v.(fiber.Handler), nil\n    }\n    return \"\", nil, nil, fmt.Errorf(\"unsupported Use arg: %T\", arg)\n}","typeGuard":"func isUseableArg(v any) bool {\n    switch v.(type) {\n    case string, *fiber.App, fiber.Handler, func(*fiber.Ctx) error, []string:\n        return true\n    }\n    return false\n}","tryCatchPattern":"// Wrap app.Use calls in tests to catch type mistakes\nfunc useSafely(app *fiber.App, args ...any) (err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"use: %v\", r) }\n    }()\n    app.Use(args...)\n    return nil\n}","preventionTips":["Mount sub-apps with app.Use(prefix, subApp) explicitly.","Apply middleware on the Group via group.Use, not on the parent app.","Add a smoke test that builds the router to catch Use-type panics."],"tags":["routing","middleware","type-mismatch","startup"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}