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| Name | Type | Description |
|---|---|---|
kind | int | The kind of error; one of the Kind constants. |
message | str = "" | The error message, in English. |
position | int = -1 | Where the error is in the text, in bytes, or -1 when it is not known. |
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
endConstants and variables
KindNone
const KindNone: i32 = 0The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindUnexpectedEOF
const KindUnexpectedEOF: i32 = 1The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindUnexpectedToken
const KindUnexpectedToken: i32 = 2The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindInvalidUTF8
const KindInvalidUTF8: i32 = 3The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindInvalidNumber
const KindInvalidNumber: i32 = 4The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindDepthExceeded
const KindDepthExceeded: i32 = 5The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindTypeMismatch
const KindTypeMismatch: i32 = 6The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindKeyNotFound
const KindKeyNotFound: i32 = 7The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindTrailingData
const KindTrailingData: i32 = 8The example for this item is shown with the ones above: encoding-encerr-Error.salam
KindCustom
const KindCustom: i32 = 99The example for this item is shown with the ones above: encoding-encerr-Error.salam
Functions
NoError
func NoError(): EncodingErrorThe example for this item is shown with the ones above: encoding-encerr-Error.salam
NewError
func NewError(kind: int, message: str, position: int): EncodingErrorThe example for this item is shown with the ones above: encoding-encerr-Error.salam
ErrUnexpectedEOF
func ErrUnexpectedEOF(position: int): EncodingErrorThe example for this item is shown with the ones above: encoding-encerr-Error.salam
ErrUnexpectedToken
func ErrUnexpectedToken(message: str, position: int): EncodingErrorThe example for this item is shown with the ones above: encoding-encerr-Error.salam
ErrTypeMismatch
func ErrTypeMismatch(expected: str, position: int): EncodingErrorThe example for this item is shown with the ones above: encoding-encerr-Error.salam
IsError
func IsError(e: EncodingError): boolThe example for this item is shown with the ones above: encoding-encerr-Error.salam