Standard library · Encoding and data

encoding/encerr

The shared error of the format readers: a kind, a message and a position in the text.

import encoding.encerr

The functions that read JSON, YAML, env files and MessagePack report their errors in an EncodingError. Build one with NoError, pass it to the function, and then check IsError.

Types

EncodingError

struct EncodingError
An error from reading or writing a format.
NameTypeDescription
kindintThe kind of error; one of the Kind constants.
messagestr = ""The error message, in English.
positionint = -1Where the error is in the text, in bytes, or -1 when it is not known.
encoding-encerr-Error.salam
import encoding.encerr
import encoding.json

func main:
    mut e := encerr.NoError()
    println encerr.IsError(e), e.kind == encerr.KindNone
    _ := json.Decode(`{"a": }`, e)
    println encerr.IsError(e), e.kind, e.position, e.message
    custom := encerr.NewError(encerr.KindCustom, "a negative value is not allowed", 12)
    println custom.kind, custom.message, custom.position
    println encerr.ErrUnexpectedEOF(40).kind == encerr.KindUnexpectedEOF
    println encerr.ErrUnexpectedToken("expected ':'", 3).kind == encerr.KindUnexpectedToken
    println encerr.ErrTypeMismatch("a number", 5).message
    println encerr.KindInvalidUTF8, encerr.KindInvalidNumber, encerr.KindDepthExceeded, encerr.KindTypeMismatch, encerr.KindKeyNotFound, encerr.KindTrailingData
end
Outputfalse true true 2 6 invalid value 99 a negative value is not allowed 12 true true expected a number 3 4 5 6 7 8

Constants and variables

KindUnexpectedEOF

const KindUnexpectedEOF: i32 = 1
The text ended sooner than expected.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindUnexpectedToken

const KindUnexpectedToken: i32 = 2
A character or token turned up out of place.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindInvalidUTF8

const KindInvalidUTF8: i32 = 3
The text is not valid UTF-8.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindInvalidNumber

const KindInvalidNumber: i32 = 4
An invalid number.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindDepthExceeded

const KindDepthExceeded: i32 = 5
The nesting went past the allowed depth.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindTypeMismatch

const KindTypeMismatch: i32 = 6
The value's type does not match the expected one.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindKeyNotFound

const KindKeyNotFound: i32 = 7
A required key was not found.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindTrailingData

const KindTrailingData: i32 = 8
Extra data turned up after the end of the value.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

KindCustom

const KindCustom: i32 = 99
An error defined by the program.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

Functions

NoError

func NoError(): EncodingError
The empty error, meaning "no error".

The example for this item is shown with the ones above: encoding-encerr-Error.salam

NewError

func NewError(kind: int, message: str, position: int): EncodingError
A new error with a kind, a message and a position.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

ErrUnexpectedEOF

func ErrUnexpectedEOF(position: int): EncodingError
An unexpected end of text error at the given position.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

ErrUnexpectedToken

func ErrUnexpectedToken(message: str, position: int): EncodingError
An out of place token error with a message.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

ErrTypeMismatch

func ErrTypeMismatch(expected: str, position: int): EncodingError
A type mismatch error; its message names the expected type.

The example for this item is shown with the ones above: encoding-encerr-Error.salam

IsError

func IsError(e: EncodingError): bool
True when an error has been recorded.

The example for this item is shown with the ones above: encoding-encerr-Error.salam