Skip to content

error

The error package builds the structured errors that Kiwi’s try/catch understands, inspects caught errors, and provides guard helpers that throw when a check fails. Error Handling explains how errors are thrown and caught; this page is the package reference.

A structured error is a hashmap with an error key (the type name) and a message key:

e = error::NotFoundError("user 42")
println e # {"error": "NotFoundError", "message": "user 42"}
try
throw e
catch caught
println error::type_of(caught) # NotFoundError
println error::message_of(caught) # user 42
println error::is_type(caught, "KeyError") # false
end

type_of, message_of and is_type also accept the plain strings that a two-variable catch type, message gives you.

error::new(type, message) makes an error with any type name. The named constructors take an optional message:

ConstructorDefault message
ArgumentErrorInvalid argument.
AssertionErrorAssertion failed.
IndexErrorIndex out of bounds.
IOErrorI/O error.
InvalidOperationInvalid operation.
InvalidRangeInvalid range.
KeyErrorKey not found.
NetworkErrorNetwork error.
NotFoundErrorNot found.
NotImplementedErrorNot implemented.
OverflowErrorOverflow.
ParseErrorParse error.
PermissionErrorPermission denied.
TimeoutErrorOperation timed out.
TypeErrorType error.
ValueErrorInvalid value.
fn set_volume(level)
error::expect_type(level, "integer", "level")
error::assert(level.between(0, 10), "level must be 0..10")
level
end
println set_volume(7) # 7
try
set_volume("loud")
catch e
println e["message"] # Expected level to be integer, got string.
end
FunctionThrows when
assert(condition, message)condition is false (AssertionError)
expect_type(value, type, name)value is not of type type (TypeError)
expect_not_null(value, name)value is null (ValueError)
error::new(error_type: string = "Error", error_message: string = "An error occurred."): hashmap

Creates a structured error hashmap.

ParameterTypeDefaultDescription
error_typestring"Error"The error type name (default "Error").
error_messagestring"An error occurred."The error message (default "An error occurred.").

Returns hashmap — A hashmap with error and message keys.

throw error::new("NotFound", "record 42 does not exist")
error::type_of(err): string

Returns the type string of a caught error.

ParameterTypeDefaultDescription
erranyA caught error hashmap (from single-param catch e) or a plain string.

Returns string — The error key if err is a structured hashmap, otherwise err as a string.

try
throw error::NotFoundError("item missing")
catch e
println error::type_of(e) # NotFoundError
println error::message_of(e) # item missing
end
error::message_of(err): string

Returns the message of a caught error.

ParameterTypeDefaultDescription
erranyA caught error hashmap (from single-param catch e) or a plain string.

Returns string — The message key if err is a structured hashmap, otherwise err as a string.

error::is_type(err, error_type: string): boolean

Returns true if the caught error matches a specific type name.

ParameterTypeDefaultDescription
erranyA caught error hashmap (from single-param catch e) or a plain string.
error_typestringThe type name to match against.

Returns boolean — true if the error type matches.

# single-param catch — e is a hashmap
try
throw error::KeyError("missing_key")
catch e
if error::is_type(e, "KeyError")
println "handle missing key"
end
end
# two-param catch — pass the type string directly
try
throw error::KeyError("missing_key")
catch type, msg
if error::is_type(type, "KeyError")
println "handle missing key: ${msg}"
end
end
error::assert(condition: boolean, msg: string = "Assertion failed.")

Throws an AssertionError if condition is false.

ParameterTypeDefaultDescription
conditionbooleanThe condition to check.
msgstring"Assertion failed."The error message (default "Assertion failed.").

Throws AssertionError — If condition is false.

error::assert(x > 0, "x must be positive")
error::expect_type(val, expected_type: string, name: string = "value")

Throws a TypeError if val is not of the expected type.

ParameterTypeDefaultDescription
valanyThe value to check.
expected_typestringThe expected type name (e.g., "integer", "string").
namestring"value"An optional label for the value, used in the error message.

Throws TypeError — If val.type() does not match expected_type.

fn add(a, b)
error::expect_type(a, "integer", "a")
error::expect_type(b, "integer", "b")
return a + b
end
error::expect_not_null(val, name: string = "value")

Throws a ValueError if val is null.

ParameterTypeDefaultDescription
valanyThe value to check.
namestring"value"An optional label for the value, used in the error message.

Throws ValueError — If val == null.

fn process(record)
error::expect_not_null(record, "record")
# ...
end
error::ArgumentError(msg: string = "Invalid argument."): hashmap

Creates an ArgumentError hashmap.

ParameterTypeDefaultDescription
msgstring"Invalid argument."The error message (default "Invalid argument.").

Returns hashmap — A structured error hashmap.

error::AssertionError(msg: string = "Assertion failed."): hashmap

Creates an AssertionError hashmap.

ParameterTypeDefaultDescription
msgstring"Assertion failed."The error message (default "Assertion failed.").

Returns hashmap — A structured error hashmap.

error::IndexError(msg: string = "Index out of bounds."): hashmap

Creates an IndexError hashmap.

ParameterTypeDefaultDescription
msgstring"Index out of bounds."The error message (default "Index out of bounds.").

Returns hashmap — A structured error hashmap.

error::IOError(msg: string = "I/O error."): hashmap

Creates an IOError hashmap.

ParameterTypeDefaultDescription
msgstring"I/O error."The error message (default "I/O error.").

Returns hashmap — A structured error hashmap.

error::KeyError(msg: string = "Key not found."): hashmap

Creates a KeyError hashmap.

ParameterTypeDefaultDescription
msgstring"Key not found."The error message (default "Key not found.").

Returns hashmap — A structured error hashmap.

error::NetworkError(msg: string = "Network error."): hashmap

Creates a NetworkError hashmap.

ParameterTypeDefaultDescription
msgstring"Network error."The error message (default "Network error.").

Returns hashmap — A structured error hashmap.

error::NotFoundError(msg: string = "Not found."): hashmap

Creates a NotFoundError hashmap.

ParameterTypeDefaultDescription
msgstring"Not found."The error message (default "Not found.").

Returns hashmap — A structured error hashmap.

error::NotImplementedError(msg: string = "Not implemented."): hashmap

Creates a NotImplementedError hashmap.

ParameterTypeDefaultDescription
msgstring"Not implemented."The error message (default "Not implemented.").

Returns hashmap — A structured error hashmap.

error::OverflowError(msg: string = "Overflow."): hashmap

Creates an OverflowError hashmap.

ParameterTypeDefaultDescription
msgstring"Overflow."The error message (default "Overflow.").

Returns hashmap — A structured error hashmap.

error::ParseError(msg: string = "Parse error."): hashmap

Creates a ParseError hashmap.

ParameterTypeDefaultDescription
msgstring"Parse error."The error message (default "Parse error.").

Returns hashmap — A structured error hashmap.

error::PermissionError(msg: string = "Permission denied."): hashmap

Creates a PermissionError hashmap.

ParameterTypeDefaultDescription
msgstring"Permission denied."The error message (default "Permission denied.").

Returns hashmap — A structured error hashmap.

error::TimeoutError(msg: string = "Operation timed out."): hashmap

Creates a TimeoutError hashmap.

ParameterTypeDefaultDescription
msgstring"Operation timed out."The error message (default "Operation timed out.").

Returns hashmap — A structured error hashmap.

error::TypeError(msg: string = "Type error."): hashmap

Creates a TypeError hashmap.

ParameterTypeDefaultDescription
msgstring"Type error."The error message (default "Type error.").

Returns hashmap — A structured error hashmap.

error::ValueError(msg: string = "Invalid value."): hashmap

Creates a ValueError hashmap.

ParameterTypeDefaultDescription
msgstring"Invalid value."The error message (default "Invalid value.").

Returns hashmap — A structured error hashmap.

error::InvalidRange(msg: string = "Invalid range."): hashmap

Creates an InvalidRange hashmap.

ParameterTypeDefaultDescription
msgstring"Invalid range."The error message (default "Invalid range.").

Returns hashmap — A structured error hashmap.

error::InvalidOperation(msg: string = "Invalid operation."): hashmap

Creates an InvalidOperation hashmap.

ParameterTypeDefaultDescription
msgstring"Invalid operation."The error message (default "Invalid operation.").

Returns hashmap — A structured error hashmap.