Language
Error Handling
Tea models recoverable failures with typed error declarations, `throw`, and `catch` plus `case` arms.
Tea treats recoverable failures as typed values. You declare error variants up front, throw them from functions that advertise failure, and recover with catch plus case arms.
Typed errors and throws
Start by naming the failure cases a function can produce. That keeps the error surface explicit and makes it clear to callers what can go wrong.
error ConfigError {
Missing(key: String)
}
def require_env(key: String) -> String ! ConfigError.Missing
const value = @env(key)
if value == nil
throw ConfigError.Missing(key)
end
value!
endRecover errors as values
When you want to turn an error into a regular value, catch it and match the specific variants you care about. Any unmatched path can fall through to a final wildcard arm.
def describe(path: String) -> String
try read(path) catch err
case is DataError.Missing
`missing:${err.path}`
case is DataError.Permission
"denied"
case _
"unexpected"
end
endPractical guidance
Declare error variants up front with
error Name { ... }
.
Use
throwfrom functions that advertise failure in their signature.- Recover with
catch errand explicitcasearms.