{"record":{"id":"035355b0bdcbb2ae","repo":"crowdsecurity/crowdsec","slug":"basename-want-string-got-t","errorCode":null,"errorMessage":"basename: want string, got %T","messagePattern":"basename: want string, got %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hubtest/helpers.go","lineNumber":18,"sourceCode":"package hubtest\n\nimport (\n\t\"errors\"\n\t\"fmt\"\n\t\"path/filepath\"\n)\n\nfunc basename(params ...any) (any, error) {\n\t// keep nilaway happy\n\tif len(params) == 0 || params[0] == nil {\n\t\treturn \"\", errors.New(\"basename: missing argument\")\n\t}\n\n\t// keep forcetypeassert happy\n\ts, ok := params[0].(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"basename: want string, got %T\", params[0])\n\t}\n\n\treturn filepath.Base(s), nil\n}\n","sourceCodeStart":1,"sourceCodeEnd":23,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/hubtest/helpers.go#L1-L23","documentation":"basename is a custom expr helper used in hubtest expressions. It requires exactly one string argument; if params[0] is not a string it returns 'basename: want string, got <type>'. This means an expression called basename() with a non-string value (nil, number, map, etc.).","triggerScenarios":"A parser/scenario expression evaluates basename with a non-string operand — e.g. basename(evt.Parsed.field) where the field is absent (nil) or holds a non-string type at runtime.","commonSituations":"Assert or parser expression referencing a field that is sometimes missing from the parsed event; passing an integer/bool result of another helper into basename; typo making the expression evaluate to nil.","solutions":["Ensure the argument is a string: wrap with a conversion or use String() where applicable in the expression","Guard the expression against a missing field (check field presence before calling basename)","Fix the expression in the .expr file so it passes evt.Parsed.<field> which is populated as a string"],"exampleFix":"// before\nbasename(evt.Parsed.timestamp)   # timestamp is nil\n// after\nbasename(evt.Parsed.filename)    # filename is a string field","handlingStrategy":"type-guard","validationCode":"// in the expression, guard before use:\n// evt.Parsed.filename != nil && String(evt.Parsed.filename) != \"\"","typeGuard":"func isString(v interface{}) bool { _, ok := v.(string); return ok }\n// call basename only when isString(evt.Parsed.field)","tryCatchPattern":"_, err := hubtest.EvalExpr(expr, evt)\nif err != nil && strings.Contains(err.Error(), \"want string\") {\n    return fmt.Errorf(\"basename() arg must be a string: %w\", err)\n}","preventionTips":["Ensure the referenced evt.Parsed field is populated before basename()","Never pass results of numeric/bool helpers into basename","Test expressions against edge events where fields may be missing"],"tags":["expr","type-mismatch","hubtest","helpers"],"backgroundTag":"type-mismatch","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}