Error.wrap
func Error.wrap(e: Error, context: string): ErrorError.wrap returns a new Error whose message is context, ": ", and the message of e, leaving e untouched. An empty context is joined like any other.
Details
- The result keeps only the joined text; nothing leads from it back to
e.
Basic examples
Put the operation in front of the message:
let e = Error{ message: "no such file" }
Error.wrap(e, "load config") // Error{ message: "load config: no such file" }Read the joined message:
let e = Error{ message: "connection refused" }
Error.wrap(e, "connect db.local:5432").message // "connect db.local:5432: connection refused"Scope
Each wrap adds one more context in front, so the outermost caller reads first:
let e = Error{ message: "timeout" }
e | Error.wrap("fetch user") | Error.wrap("render page") // Error{ message: "render page: fetch user: timeout" }An empty context still adds the separator:
Error.wrap(Error{ message: "timeout" }, "") // Error{ message: ": timeout" }An empty message keeps the separator too:
Error.wrap(Error{ message: "" }, "fetch") // Error{ message: "fetch: " }Call it through .:
let e = Error{ message: "refused" }
e.wrap("connect") // Error{ message: "connect: refused" }Applications
Turn an error type of your own into an Error for a person to read, inside Result.mapErr. For a Result that already holds an Error, Result.wrapErr is the short form:
type BadLine = { line: int }
func parseSettings(text: string): Result[string, BadLine] {
if text == "" {
return Result.err(BadLine{ line: 1 })
}
Result.ok(text)
}
let parsed = parseSettings("")
| Result.mapErr({ Error{ message: "bad line ${x.line}" }.wrap("parse settings") })
if parsed is Err {
println(parsed.error.message)
}parse settings: bad line 1Wrap an error that is not in a Result, such as one a check returns, with the field it is about:
func checkPort(port: int): Error | null {
if port < 1 || port > 65535 {
return Error{ message: "${port} is out of range" }
}
null
}
let problem = checkPort(70000)
if problem != null {
println(problem.wrap("port").message)
}port: 70000 is out of rangeProperties and relations
Result.wrapErr applies Error.wrap to the error of an Err:
let e = Error{ message: "timeout" }
let failed: Result[int, Error] = Result.err(e)
let expected: Result[int, Error] = Result.err(Error.wrap(e, "fetch"))
assert(Result.wrapErr(failed, "fetch") == expected)The message is the context, : , and the old message:
let e = Error{ message: "timeout" }
assert(Error.wrap(e, "fetch").message == "fetch" + ": " + e.message)Wrapping leaves the original error as it was:
let e = Error{ message: "timeout" }
let wrapped = e.wrap("fetch")
e // Error{ message: "timeout" }Possible issues
e must be an Error. An error type of your own is a compile error; build an Error from it first:
type NotFound = { id: int }
let wrapped = Error.wrap(NotFound{ id: 7 }, "load profile")
// error: cannot use NotFound where Error is expectedThe result is a new record that holds only text. Record.same with the original is false, and the original cannot be recovered from it. Branch on an error type of your own instead:
let e = Error{ message: "timeout" }
Record.same(e.wrap("fetch"), e) // falseError.wrap adds : itself, so a context that ends in a colon gets two:
Error.wrap(Error{ message: "no such file" }, "failed to load config:") // Error{ message: "failed to load config:: no such file" }Name the operation and its argument in lowercase, with no trailing punctuation: load config.
