Skip to content

EncodingError

Built-in class · Extends RuntimeError

An EncodingError is what Emerald raises when text or bytes can’t be converted from one form to another: bytes that aren’t valid UTF-8 read as text, hexadecimal text with a stray character, or Base64 text that isn’t Base64.

const data = Bytes.from_list([255])
try {
print(data.to_string())
}
catch error: EncodingError {
print(error.message)
}
Output
these Bytes are not valid UTF-8 text

An EncodingError is a RuntimeError, so catch error: RuntimeError catches it too.

message: String

What went wrong, in words, saying which character or position was at fault where there is one.

Conversion Cause
Bytes.to_string The bytes aren’t valid UTF-8 text
Bytes.from_hex An odd number of digits, or a character that isn’t a hexadecimal digit
Base64.decode A character, length, padding, or ending that Base64 doesn’t allow

Each has a _maybe form that gives nothing instead, for input that may well be wrong, such as text a person typed.