Panic.Frame
type Panic.Frame = { function: string, path: string, line: int, column: int }Panic.Frame is one frame of a panic's trace: the function, the path of its module, and the line and column, counted from 1, of the panic in the first frame and of a call in the others.
Details
Panic.catchgives aPanicwhosetraceis an array of frames, innermost first:trace[0]is where the panic began, and each later frame is the call that led to the one before it.- The trace does not stop at the function
Panic.catchcalled. It goes on past that call to the goroutine's root, so it also holds the frames of the code around thecatch. Panic.raisekeeps the trace as it is, so the frames still point at the original panic, not at theraise.
Basic examples
Catch a panic and read where it began:
func half(n: int): int {
if n % 2 != 0 { panic("odd: ${n}") }
n / 2
}
let r = Panic.catch({ half(7) })
if r is Err {
let top = r.error.trace[0]
println("${r.error.message} in ${top.function}, ${top.path} line ${top.line}")
}odd: 7 in half, main.ant line 2Write one with a literal, and read its fields:
let f = Panic.Frame{ function: "load", path: "config.ant", line: 12, column: 5 }
f // Panic.Frame{ function: "load", path: "config.ant", line: 12, column: 5 }
f.function // "load"
f.line // 12Scope
The frame after the first is the caller, at the line of its call:
func check(age: int): int {
assert(age >= 0, "age is never negative")
age
}
func register(age: int): int {
check(age)
}
let r = Panic.catch({ register(-1) })
if r is Err {
let trace = r.error.trace
println("${trace[0].function} line ${trace[0].line}")
println("${trace[1].function} line ${trace[1].line}")
}check line 2
register line 7A frame in an imported module names that module's path:
export func area(width: int, height: int): int {
if width < 0 || height < 0 { panic("negative size") }
width * height
}import "./geo.ant" { area }
let r = Panic.catch({ area(-1, 2) })
if r is Err {
let top = r.error.trace[0]
println("${top.function} in ${top.path}")
}area in geo.antAnnotate an array of frames with Array[Panic.Frame]:
let f = Panic.Frame{ function: "parse", path: "json.ant", line: 40, column: 9 }
let frames: Array[Panic.Frame] = [f, Panic.Frame{ ...f, line: 41 }]
len(frames) // 2String.from and Json.stringify show every field:
let f = Panic.Frame{ function: "f", path: "a.ant", line: 3, column: 1 }
String.from(f) // "Panic.Frame{ function: \"f\", path: \"a.ant\", line: 3, column: 1 }"
Json.stringify(f) // "{\"function\":\"f\",\"path\":\"a.ant\",\"line\":3,\"column\":1}"Applications
Format a frame the way file:line:column locations are written:
func location(f: Panic.Frame): string {
"${f.path}:${f.line}:${f.column}"
}
location(Panic.Frame{ function: "run", path: "jobs.ant", line: 8, column: 3 }) // "jobs.ant:8:3"Run each job under Panic.catch and report where a failing job panicked, then go on with the next:
type Job = { id: int, items: Array[int] }
func firstItem(job: Job): int {
job.items[0]
}
let jobs = [Job{ id: 1, items: [5] }, Job{ id: 2, items: [] }, Job{ id: 3, items: [7] }]
for job in jobs {
let r = Panic.catch({ firstItem(job) })
if r is Err {
let top = r.error.trace[0]
println("job ${job.id} failed in ${top.function} at line ${top.line}")
} else {
println("job ${job.id}: ${r.value}")
}
}job 1: 5
job 2 failed in firstItem at line 4
job 3: 7Log the innermost frame as JSON, for a log collector:
type LogLine = { message: string, function: string, line: int }
func fail() {
panic("disk full")
}
let r = Panic.catch(fail)
if r is Err {
let top = r.error.trace[0]
println(Json.stringify(LogLine{ message: r.error.message, function: top.function, line: top.line }))
}{"message":"disk full","function":"fail","line":4}Properties and relations
Frames compare by their fields:
let f = Panic.Frame{ function: "a", path: "a.ant", line: 1, column: 1 }
assert(f == Panic.Frame{ ...f })
assert(f != Panic.Frame{ ...f, line: 2 })Every frame of a caught trace counts its line and column from 1:
func deep(n: int): int {
if n == 0 { panic("bottom") }
deep(n - 1)
}
let r = Panic.catch({ deep(3) })
if r is Err {
assert(r.error.trace.every({ x.line >= 1 && x.column >= 1 }))
}Panic.raise keeps the trace, so catching a raised panic gives the same frames:
func boom() {
panic("boom")
}
let first = Panic.catch(boom)
if first is Err {
let again = Panic.catch({ Panic.raise(first.error) })
if again is Err {
assert(again.error.trace == first.error.trace)
}
}Possible issues
Frame alone is not a name. Write Panic.Frame:
let f = Frame{ function: "a", path: "a.ant", line: 1, column: 1 }
// error: unknown type Frame; did you mean Panic.Frame?A literal gives every field:
let f = Panic.Frame{ function: "a", path: "a.ant", line: 1 }
// error: Panic.Frame literal is missing the field columnThe trace is innermost first. trace[0] is the function that panicked, not the one that called it:
func inner() {
panic("deep")
}
func outer() {
inner()
}
let r = Panic.catch(outer)
if r is Err {
println(r.error.trace[0].function)
println(r.error.trace[1].function)
}inner
outerSee also
Panic · Panic.catch · Panic.raise · panic · assert
