{"record":{"id":"4df424aab0069a5a","repo":"gofr-dev/gofr","slug":"failed-to-parse-json-config-file-s-w","errorCode":null,"errorMessage":"failed to parse JSON config file %s: %w","messagePattern":"failed to parse JSON config file (.+?): %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/gofr/rbac/config.go","lineNumber":151,"sourceCode":"// Dependencies (logger, metrics, tracer) are optional and can be set after loading.\nfunc LoadPermissions(path string, logger datasource.Logger, metrics container.Metrics, tracer trace.Tracer) (*Config, error) {\n\tdata, err := os.ReadFile(path)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to read RBAC config file %s: %w\", path, err)\n\t}\n\n\tvar config Config\n\n\t// Detect file format by extension\n\text := strings.ToLower(filepath.Ext(path))\n\tswitch ext {\n\tcase \".yaml\", \".yml\":\n\t\tif err := yaml.Unmarshal(data, &config); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse YAML config file %s: %w\", path, err)\n\t\t}\n\tcase \".json\", \"\":\n\t\tif err := json.Unmarshal(data, &config); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse JSON config file %s: %w\", path, err)\n\t\t}\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unsupported config file format: %s (supported: .json, .yaml, .yml): %w\", ext, errUnsupportedFormat)\n\t}\n\n\t// Set dependencies\n\tconfig.Logger = logger\n\tconfig.Metrics = metrics\n\tconfig.Tracer = tracer\n\n\t// Initialize mux router for pattern matching\n\t// Use StrictSlash(false) to match the application router's behavior\n\tconfig.muxRouter = mux.NewRouter().StrictSlash(false)\n\n\t// Validate config before processing\n\tif err := config.validate(); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid RBAC config: %w\", err)\n\t}","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/rbac/config.go#L133-L169","documentation":"LoadPermissions failed to parse the RBAC config file as JSON. json.Unmarshal returned an error (invalid syntax, wrong types, trailing data), so the RBAC configuration cannot be loaded and startup fails. The file path is embedded for locating the file.","triggerScenarios":"Calling EnableRBAC/LoadPermissions with a .json file (or no extension) containing invalid JSON: trailing commas, single quotes, unquoted keys, comments, BOM, or values of the wrong type for the Config struct.","commonSituations":"Config generated by hand or copied from docs with comments; an environment-specific pipeline emitted malformed JSON; a file with no extension is actually YAML; type mismatch like permissions as a string instead of an array.","solutions":["Run the file through a JSON validator (jq or jsonlint) and fix the syntax error at the reported offset.","Remove JSON-illegal constructs: comments, trailing commas, single quotes.","Check field types against the Config struct (e.g. requiredPermissions must be an array).","If the content is YAML, rename to .yaml/.yml so the YAML branch parses it."],"exampleFix":"// before (rbac.json)\n{ \"endpoints\": [ { \"path\": \"/api/users\", \"requiredPermissions\": [\"users:read\"], } ] }\n// after\n{ \"endpoints\": [ { \"path\": \"/api/users\", \"requiredPermissions\": [\"users:read\"] } ] }","handlingStrategy":"validation","validationCode":"data, err := os.ReadFile(path)\nif err != nil { return err }\next := strings.ToLower(filepath.Ext(path))\nif ext == \".json\" || ext == \"\" {\n    var probe map[string]any\n    if err := json.Unmarshal(data, &probe); err != nil {\n        return fmt.Errorf(\"rbac json %s invalid: %w\", path, err)\n    }\n}","typeGuard":null,"tryCatchPattern":"config, err := rbac.LoadPermissions(path, logger, metrics, tracer)\nif err != nil && strings.Contains(err.Error(), \"failed to parse JSON\") {\n    logger.Fatalf(\"invalid JSON in %s: %v\", path, err)\n}","preventionTips":["Run jq over generated JSON configs in CI.","Forbid comments and trailing commas (no JSON5 in .json files).","Generate config from a typed struct via json.Marshal instead of hand-editing.","Keep extension-less config files strictly JSON."],"tags":["go","json","config","rbac","parse-error"],"backgroundTag":"config-file-parse-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}