Skip to content
LexerLangv0.1.0

Error handling

try / catch / finally, $e.type, and the signals that are not caught.

LexerLang
try
    $name = $req.query.name
catch($e)
    out("error: " + $e.message)
finally
    out("in every case")
end try

catch and finally are optional, but at least one must be present.

The parameter can be written three ways; all three are the same:

LexerLang
catch($e)
catch $e
catch

The caught value#

It is an object: {type, message, file, line, column}.

LexerLang
try
    session.set("counter", 1)
catch($e)
    if $e.type == "SessionConflictError" then
        out("session conflict, retrying")
    end if
end try

type 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.

LexerLang
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 func

If 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:

LexerLang
func validate($data) as object
    if $data.get("email") == null then
        return {ok: false, error: "email is required"}
    end if
    return {ok: true}
end func

The startup run#

This is an easy trap to miss:

LexerLang
$j = $req.json
out($j.get("name"))        # at startup $j is null -> error

Two safe patterns:

LexerLang
if $req.method == http_request.types.POST then
    out($req.json.get("name"))
end if
LexerLang
try
    out($req.json.get("name"))
catch($e)
    out("no data")
end try

The form("name") / get("q") / header(...) helpers already return null, so they need no guard.

The error page in production#

Terminal
lexer serve app.lexer --production
Text
development:  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.

Next#

Routing.