{"record":{"id":"38810891dd2f4992","repo":"affaan-m/ECC","slug":"load-config-s-w","errorCode":null,"errorMessage":"load config %s: %w","messagePattern":"load config (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"skills/golang-patterns/SKILL.md","lineNumber":103,"sourceCode":"    return &Result{Data: data}, nil\n}\n\n// Bad: Returns interface (hides implementation details unnecessarily)\nfunc ProcessData(r io.Reader) (io.Reader, error) {\n    // ...\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","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/skills/golang-patterns/SKILL.md#L85-L121","documentation":"Wrapped error from LoadConfig in golang-patterns. LoadConfig calls os.ReadFile(path); on any read failure it wraps the error as fmt.Errorf(\"load config %s: %w\", path, err). The message indicates the file at `path` could not be read; the underlying os.PathError (open/read) is preserved for the caller.","triggerScenarios":"Calling LoadConfig(path) when os.ReadFile(path) returns a non-nil error. Concrete triggers: the path does not exist (fs.ErrNotExist), permission denied (fs.ErrPermission), a path component is not a directory, or the path is empty.","commonSituations":"Wrong config path passed at startup (relative path resolved against an unexpected CWD); the file was not included in the container image or deployment artifact; permissions were stripped after a deploy; the env var pointing at the config is unset so path is empty or default.","solutions":["Print the resolved absolute path before loading to catch CWD-relative surprises.","Verify the file exists and is readable by the running user (stat + access check).","Use an absolute path or resolve relative to the executable / a known config dir.","Ensure the deploy artifact actually includes the config file.","Fail fast at startup with a clear message rather than letting the wrapped error propagate."],"exampleFix":"// before\ncfg, err := LoadConfig(path)\nif err != nil {\n    log.Fatal(err)\n}\n\n// after\ncfg, err := LoadConfig(path)\nif err != nil {\n    if errors.Is(err, fs.ErrNotExist) {\n        log.Fatalf(\"config file not found at %s; set --config or CONFIG_PATH\", path)\n    }\n    log.Fatalf(\"loading config %s: %v\", path, err)\n}","handlingStrategy":"validation","validationCode":"func ensureConfigReadable(path string) error {\n    abs, err := filepath.Abs(path)\n    if err != nil {\n        return err\n    }\n    info, err := os.Stat(abs)\n    if err != nil {\n        return err\n    }\n    if info.IsDir() {\n        return fmt.Errorf(\"%s is a directory, not a file\", abs)\n    }\n    if info.Mode().Perm()&0400 == 0 {\n        return fmt.Errorf(\"%s is not readable\", abs)\n    }\n    return nil\n}","typeGuard":"func isMissingFile(err error) bool {\n    return errors.Is(err, fs.ErrNotExist)\n}\n\nfunc isPermissionDenied(err error) bool {\n    return errors.Is(err, fs.ErrPermission)\n}","tryCatchPattern":"cfg, err := LoadConfig(path)\nif err != nil {\n    if isMissingFile(err) {\n        log.Fatalf(\"config not found at %s; set --config\", path)\n    }\n    if isPermissionDenied(err) {\n        log.Fatalf(\"no read permission on %s\", path)\n    }\n    log.Fatalf(\"load config: %v\", err)\n}","preventionTips":["Resolve the config path to absolute at startup and log it.","Include the config file in your deploy artifact and verify it in CI.","Default to a stable, well-known config directory.","Fail fast at startup instead of letting read errors surface later."],"tags":["go","config","filesystem","error-wrapping"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}