Skip to content

Error handling and function calls

Compare
Choose a tag to compare
@christophhagen christophhagen released this 18 Jul 14:37
· 116 commits to master since this release

Error cases

This release ensures that all errors thrown for encoding and decoding are actually of type BinaryEncodingError and BinaryDecodingError, by introducing additional error cases for unknown errors, as well as equivalents to the Swift EncodingError and DecodingError types.

BinaryEncodingError gets the new cases:

case invalidValue(Any, EncodingError.Context)
case unknownError(Error)

while `BinaryDecodingError gets the new cases:

case dataCorrupted(DecodingError.Context)
case typeMismatch(Any.Type, DecodingError.Context)
case valueNotFound(Any.Type, DecodingError.Context)
case unknownError(Error)

Encoding function calls

This release also relaxes the encoding function definitions, switching from:

public func encode<T>(_ value: T) throws -> Data where T: Encodable

to:

public func encode(_ value: Encodable) throws -> Data

which allows more calls, such as:

let values: [Encodable] = ["Some", 123, true]

// Previously not possible
let data = try values.map { try BinaryEncoder.encode($0) }