Error.NotExist
type Error.NotExist = {}Error.NotExist is the kind of an error for a path that nothing is at.
Details
- The library sets it when nothing is at a path a call names: a read, a stat, an open or a remove of a missing path, a write into a directory that is missing, a
Cmdwhosediror program path is missing. - A path that leads through a file, such as
notes.txt/today, is notError.NotExist: itskindisnull. - A
Cmdthat names a program without a slash, which no directory ofPATHholds, has no kind. Ask Os.which before the call. Error.NotExistis an empty record, so the value isError.NotExist{}.
Basic examples
Use defaults when a file is missing:
let saved = File.read("theme.txt")
let theme = if saved is Err && saved.error.kind is Error.NotExist { "light" } else { try saved }
println(theme)lightThe printed error carries the kind:
println(File.read("theme.txt"))Err{ error: Error{ message: "open theme.txt: no such file or directory", kind: Error.NotExist{} } }Scope
Calls that name a missing path give it, whatever they were going to do there:
println(File.stat("draft.md"))
println(File.remove("draft.md"))
println(File.write("drafts/today.md", "x"))
println(File.rename("draft.md", "final.md"))Err{ error: Error{ message: "stat draft.md: no such file or directory", kind: Error.NotExist{} } }
Err{ error: Error{ message: "remove draft.md: no such file or directory", kind: Error.NotExist{} } }
Err{ error: Error{ message: "open drafts/today.md: no such file or directory", kind: Error.NotExist{} } }
Err{ error: Error{ message: "rename draft.md final.md: no such file or directory", kind: Error.NotExist{} } }A command whose working directory is missing gives it too:
println(Cmd.new("ls", [], dir: "nowhere").run())Err{ error: Error{ message: "chdir nowhere: no such file or directory", kind: Error.NotExist{} } }The kind survives the context a caller adds:
let r = File.read("app.json") | Result.wrapErr("load config")
if r is Err {
println(r.error.message)
println(r.error.kind is Error.NotExist)
}load config: open app.json: no such file or directory
trueApplications
Load a setting with a default, and fail on a file that is there and holds something else:
func loadPort(path: string): Result[int, Error] {
let text = File.read(path)
if text is Err && text.error.kind is Error.NotExist {
return Result.ok(8080)
}
let port = (try text).trim().toInt() ?? return Result.fail("${path}: not a port")
Result.ok(port)
}
println(loadPort("port.txt"))
try File.write("port.txt", "80x")
println(loadPort("port.txt"))Ok{ value: 8080 }
Err{ error: Error{ message: "port.txt: not a port", kind: null } }Write a file, and make its directory only when the write says it is missing:
func save(path: string, text: string): Result[void, Error] {
let written = File.write(path, text)
if written is Err && written.error.kind is Error.NotExist {
try File.mkdir(Path.dir(path))
return File.write(path, text)
}
written
}
println(save("reports/2026/today.txt", "done"))
println(try File.read("reports/2026/today.txt"))Ok{ value: void }
doneProperties and relations
File.removeAll and File.exists take a missing path as an answer, not a failure, so they give no Error.NotExist:
assert(File.removeAll("build").isOk())
assert(!File.exists("build"))Possible issues
A path that leads through a file is a different failure, with no kind:
try File.write("notes.txt", "x")
println(File.read("notes.txt/today"))Err{ error: Error{ message: "open notes.txt/today: not a directory", kind: null } }File.exists is false for a missing path and also for one it cannot reach. When the difference matters, test the kind of File.stat's Err:
let info = File.stat("backups/monday.tar")
if info is Err && !(info.error.kind is Error.NotExist) {
println("cannot look: ${info.error.message}")
} else {
println("there or missing")
}there or missingSee also
Error.Kind · Error.Exist · Error.Permission · Error · File.read · File.stat · File.exists · Os.which
