Error handling
try / catch / finally, $e.type, and the signals that are not caught.
try
$name = $req.query.name
catch($e)
out("error: " + $e.message)
finally
out("in every case")
end trycatch and finally are optional, but at least one must be present.
The parameter can be written three ways; all three are the same:
catch($e)
catch $e
catchThe caught value#
It is an object: {type, message, file, line, column}.
try
session.set("counter", 1)
catch($e)
if $e.type == "SessionConflictError" then
out("session conflict, retrying")
end if
end trytype lets you tell errors apart without reading the message text. Branching on the message would break silently the day the message is reworded. Errors with no declared type arrive as "RuntimeError".
What is not caught#
return, break and continue are not caught — only finally runs and the signal passes through.
func find($list) as string
for $x in $list
try
if $x.suitable then
return $x.name # does NOT fall into catch; it returns
end if
catch($e)
out("skipped")
end try
end for
return ""
end funcIf they were caught, a function's return would be silently swallowed.
finally runs in every case; if it raises its own error, that one wins.
The catch variable is visible only inside the catch block.
Raising your own errors#
raise / throw do not exist yet. Today the way to carry a failure outwards is the return value:
func validate($data) as object
if $data.get("email") == null then
return {ok: false, error: "email is required"}
end if
return {ok: true}
end funcThe startup run#
This is an easy trap to miss:
$j = $req.json
out($j.get("name")) # at startup $j is null -> errorTwo safe patterns:
if $req.method == http_request.types.POST then
out($req.json.get("name"))
end iftry
out($req.json.get("name"))
catch($e)
out("no data")
end tryThe form("name") / get("q") / header(...) helpers already return null, so they need no guard.
The error page in production#
lexer serve app.lexer --productiondevelopment: sayfa calistirilamadi: /path/app.lexer:25:19: tanimsiz degisken: $yok
production: sayfa calistirilamadi (olay: 4b17df5d3b21)The detail goes to the log file under the same id.