{"record":{"id":"b2e053d3dc484bba","repo":"affaan-m/ECC","slug":"parse-config-s-w","errorCode":null,"errorMessage":"parse config %s: %w","messagePattern":"parse config (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"skills/golang-patterns/SKILL.md","lineNumber":108,"sourceCode":"    // ...\n}\n```\n\n## Error Handling Patterns\n\n### Error Wrapping with Context\n\n```go\n// Good: Wrap errors with context\nfunc LoadConfig(path string) (*Config, error) {\n    data, err := os.ReadFile(path)\n    if err != nil {\n        return nil, fmt.Errorf(\"load config %s: %w\", path, err)\n    }\n\n    var cfg Config\n    if err := json.Unmarshal(data, &cfg); err != nil {\n        return nil, fmt.Errorf(\"parse config %s: %w\", path, err)\n    }\n\n    return &cfg, nil\n}\n```\n\n### Custom Error Types\n\n```go\n// Define domain-specific errors\ntype ValidationError struct {\n    Field   string\n    Message string\n}\n\nfunc (e *ValidationError) Error() string {\n    return fmt.Sprintf(\"validation failed on %s: %s\", e.Field, e.Message)\n}","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/skills/golang-patterns/SKILL.md#L90-L126","documentation":"Wrapped error from LoadConfig in golang-patterns, raised on the JSON decode step. After the file is read successfully, json.Unmarshal(data, &cfg) is attempted; on failure the error is wrapped as fmt.Errorf(\"parse config %s: %w\", path, err). This message specifically means: the file was readable but its contents could not be unmarshaled into the Config type.","triggerScenarios":"Calling LoadConfig(path) where os.ReadFile succeeds but json.Unmarshal fails. Concrete triggers: the file content is not valid JSON (trailing comma, unquoted keys, single quotes, comments), a JSON value's type does not match the Go struct field, duplicate keys rejected by the decoder, or a BOM/control character breaks the parser.","commonSituations":"Hand-edited config with a trailing comma or comment; struct field renamed without updating the JSON; numbers passed where strings are expected (or vice versa); an encoding glitch introduced a BOM; the file was overwritten with YAML/TOML by mistake.","solutions":["Run the file through a JSON validator or `jq .` to localize the syntax error.","Compare the JSON keys against the Config struct fields and their json tags.","Ensure the file is UTF-8 without a BOM and contains no comments.","If multiple formats are possible, confirm the loader expects JSON for this path."],"exampleFix":"// before\nvar cfg Config\nif err := json.Unmarshal(data, &cfg); err != nil {\n    return nil, fmt.Errorf(\"parse config %s: %w\", path, err)\n}\n\n// after - surface line/column for easier diagnosis\nvar cfg Config\ndec := json.NewDecoder(bytes.NewReader(data))\ndec.DisallowUnknownFields()\nif err := dec.Decode(&cfg); err != nil {\n    return nil, fmt.Errorf(\"parse config %s at offset %d: %w\", path, decodeOffset(data, dec), err)\n}","handlingStrategy":"validation","validationCode":"func validateConfigJSON(path string) error {\n    data, err := os.ReadFile(path)\n    if err != nil {\n        return err\n    }\n    dec := json.NewDecoder(bytes.NewReader(data))\n    dec.DisallowUnknownFields()\n    var v map[string]any\n    if err := dec.Decode(&v); err != nil {\n        return fmt.Errorf(\"invalid JSON: %w\", err)\n    }\n    return nil\n}","typeGuard":"func isJSONTypeError(err error) bool {\n    var te *json.UnmarshalTypeError\n    var se *json.SyntaxError\n    return errors.As(err, &te) || errors.As(err, &se)\n}","tryCatchPattern":"cfg, err := LoadConfig(path)\nif err != nil {\n    var te *json.UnmarshalTypeError\n    if errors.As(err, &te) {\n        log.Fatalf(\"config field %s got wrong type: %v\", te.Field, te)\n    }\n    var se *json.SyntaxError\n    if errors.As(err, &se) {\n        log.Fatalf(\"config syntax error at offset %d: %v\", se.Offset, se)\n    }\n    log.Fatalf(\"load config: %v\", err)\n}","preventionTips":["Lint config JSON in CI (jq . or a schema validator).","Use DisallowUnknownFields to catch typos in keys early.","Keep JSON keys and Go struct json tags in lockstep via tests.","Avoid comments and trailing commas; they are invalid JSON."],"tags":["go","config","json","parsing"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}