{"record":{"id":"312674027e244e56","repo":"OpenNHP/opennhp","slug":"could-not-open-file-v","errorCode":null,"errorMessage":"could not open file: %v","messagePattern":"could not open file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"endpoints/db/utils.go","lineNumber":134,"sourceCode":"\ntype AppParams struct {\n\tMode                    string // the mode of operation: none, encrypt and decrypt\n\tSource                  string // the path of plaintext data\n\tDsType                  string // the type of data source: stream, online and offline\n\tOutput                  string // path of output file\n\tSmartPolicy             string // path of smart policy\n\tMetadata                string // path of metadata\n\tZtdoFilePath            string // path of ztdo file when mode is decrypt\n\tZtdoId                  string // identifier of ztdo file\n\tDataPrivateKeyBase64    string\n\tAccessUrl               string // path of access url of ztdo\n\tProviderPublicKeyBase64 string\n}\n\nfunc (a *AppParams) NewSmartPolicy() (common.SmartPolicy, error) {\n\tfile, err := os.Open(a.SmartPolicy)\n\tif err != nil {\n\t\treturn common.SmartPolicy{}, fmt.Errorf(\"could not open file: %v\", err)\n\t}\n\tdefer file.Close()\n\n\tfileContentByte, err := io.ReadAll(file)\n\tif err != nil {\n\t\treturn common.SmartPolicy{}, fmt.Errorf(\"error reading file: %v\", err)\n\t}\n\n\tvar config common.SmartPolicy\n\n\terr = json.Unmarshal(fileContentByte, &config)\n\tif err != nil {\n\t\treturn common.SmartPolicy{}, fmt.Errorf(\"json parsing error: %s\", err)\n\t}\n\n\tspoId, err := utils.GenerateUUIDv4()\n\tif err != nil {\n\t\treturn common.SmartPolicy{}, fmt.Errorf(\"error generating spoId: %v\", err)","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/endpoints/db/utils.go#L116-L152","documentation":"AppParams.NewSmartPolicy loads the smart policy file whose path is given in AppParams.SmartPolicy. If os.Open fails - the file does not exist, the path is a directory, or read permission is missing - it returns an empty common.SmartPolicy with this wrapped error. This is invoked from runApp when the db app is run in encrypt/decrypt mode with a policy file.","triggerScenarios":"Calling NewSmartPolicy when: (1) the --smart-policy path does not exist or is misspelled; (2) the path points to a directory instead of a file; (3) the process lacks read permission on the policy file; (4) SmartPolicy field is empty/unset so os.Open(\"\") fails.","commonSituations":"CLI invocation from a different working directory with a relative policy path; policy file not copied into a container image; typo in the TOML/CLI argument; permissions tightened after deploy.","solutions":["Verify the path in AppParams.SmartPolicy is correct and the file exists: run `ls -l <path>`; use an absolute path to avoid cwd surprises.","Check the wrapped os error: ENOENT means missing file, EISDIR means you passed a directory, EACCES means fix permissions (chmod/chown).","Ensure SmartPolicy is non-empty before calling; validate the argument in runApp before constructing the policy.","In containers, confirm the policy file is mounted/copied into the image at the expected path."],"exampleFix":"// before: relative path breaks when cwd differs\napp.SmartPolicy = \"policy.json\"\n// after: resolve to absolute path and check first\nabs, err := filepath.Abs(policyPath)\nif err != nil { return err }\nif _, err := os.Stat(abs); err != nil { return fmt.Errorf(\"smart policy not found: %w\", err) }\napp.SmartPolicy = abs","handlingStrategy":"validation","validationCode":"if a.SmartPolicy == \"\" {\n\treturn errors.New(\"smart policy path is empty\")\n}\nst, err := os.Stat(a.SmartPolicy)\nif err != nil { return fmt.Errorf(\"smart policy missing: %w\", err) }\nif st.IsDir() { return fmt.Errorf(\"%s is a directory\", a.SmartPolicy) }","typeGuard":"func readablePolicyFile(path string) bool {\n\tst, err := os.Stat(path)\n\treturn err == nil && st.Mode().IsRegular()\n}","tryCatchPattern":"policy, err := appParams.NewSmartPolicy()\nif err != nil {\n\tif errors.Is(err, fs.ErrNotExist) {\n\t\treturn fmt.Errorf(\"smart policy file not found at %q; check the --smart-policy argument\", appParams.SmartPolicy)\n\t}\n\treturn err\n}","preventionTips":["Pass absolute paths for the smart policy; never rely on the daemon's cwd.","Stat the policy file at startup and fail fast with a clear message.","In container images, explicitly COPY/mount the policy file and verify in CI.","Check that the SmartPolicy field is populated by the CLI/TOML parsing before runApp."],"tags":["filesystem","file-not-found","config"],"backgroundTag":"file-open-failed","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}