{"record":{"id":"5329c7488e745b0e","repo":"affaan-m/ECC","slug":"get-user-s-w-5329c7","errorCode":null,"errorMessage":"get user %s: %w","messagePattern":"get user (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"skills/golang-patterns/SKILL.md","lineNumber":30,"sourceCode":"## When to Activate\n\n- Writing new Go code\n- Reviewing Go code\n- Refactoring existing Go code\n- Designing Go packages/modules\n\n## Core Principles\n\n### 1. Simplicity and Clarity\n\nGo favors simplicity over cleverness. Code should be obvious and easy to read.\n\n```go\n// Good: Clear and direct\nfunc GetUser(id string) (*User, error) {\n    user, err := db.FindUser(id)\n    if err != nil {\n        return nil, fmt.Errorf(\"get user %s: %w\", id, err)\n    }\n    return user, nil\n}\n\n// Bad: Overly clever\nfunc GetUser(id string) (*User, error) {\n    return func() (*User, error) {\n        if u, e := db.FindUser(id); e == nil {\n            return u, nil\n        } else {\n            return nil, e\n        }\n    }()\n}\n```\n\n### 2. Make the Zero Value Useful\n","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/skills/golang-patterns/SKILL.md#L12-L48","documentation":"Wrapped error from the GetUser example in golang-patterns. GetUser delegates to db.FindUser(id) and, on any non-nil return, wraps it as fmt.Errorf(\"get user %s: %w\", id, err). This is shown as the 'clear and direct' style: a single early-return that preserves the cause via %w and adds the id as context. The message surfaces whenever the underlying lookup fails for any reason.","triggerScenarios":"Calling GetUser(id) when db.FindUser returns a non-nil error. This includes not-found, validation of the id, transport errors to the store, or any failure inside FindUser that is propagated up rather than swallowed.","commonSituations":"Repository returns a sentinel not-found; id format is invalid and the store rejects it; the data layer is temporarily unavailable; a refactor changed FindUser's error semantics and the caller now bubbles an unexpected cause.","solutions":["Inspect with errors.Is against the store's sentinel errors (e.g. ErrNotFound) to decide between 404 and 500.","Validate the id (non-empty, correct shape) before calling FindUser to surface input errors earlier.","Confirm the data layer connectivity and that FindUser's contract still matches caller assumptions.","Add structured logging of the wrapped cause at the boundary so users see a generic message but operators see detail."],"exampleFix":"// before\nuser, err := GetUser(id)\nif err != nil {\n    return err\n}\n\n// after\nuser, err := GetUser(id)\nswitch {\ncase errors.Is(err, ErrNotFound):\n    return NotFoundResponse(id)\ncase err != nil:\n    log.Printf(\"get user %s failed: %v\", id, err)\n    return InternalErrorResponse()\n}","handlingStrategy":"try-catch","validationCode":"func validateUserID(id string) error {\n    if strings.TrimSpace(id) == \"\" {\n        return errors.New(\"id required\")\n    }\n    return nil\n}","typeGuard":"// Distinguish 'not found' from 'transient failure' for the caller.\nfunc isNotFound(err error) bool {\n    return errors.Is(err, ErrNotFound) || errors.Is(err, sql.ErrNoRows)\n}","tryCatchPattern":"user, err := GetUser(id)\nswitch {\ncase isNotFound(err):\n    return NotFoundResponse(id)\ncase err != nil:\n    log.Printf(\"get user %s: %v\", id, err)\n    return InternalErrorResponse()\n}","preventionTips":["Define and reuse sentinel errors in the store layer so callers can errors.Is.","Validate ids at the handler boundary before calling the service.","Never swallow the wrapped cause; always %w it upward.","Document which errors FindUser can return."],"tags":["go","error-wrapping","patterns"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}