errors.ha (1570B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use errors; 5 use io; 6 7 // Encountered invalid DER encoded data. 8 export type invalid = !void; 9 10 // Unexpected data format. 11 export type badformat = !void; 12 13 // Premature EOF. 14 export type truncated = !void; 15 16 // Data does not fit into the encoder buffer. 17 export type overflow = !void; 18 19 type asn1error = !(invalid | badformat | overflow | truncated); 20 21 // Any error within the asn1 module. 22 export type error = !(...io::error | ...asn1error); 23 24 25 // Converts an [[error]] into a user-friendly string. 26 export fn strerror(e: error) str = { 27 match (e) { 28 case invalid => 29 return "Encountered invalid DER encoded data"; 30 case badformat => 31 return "Unexpected data format"; 32 case truncated => 33 return "Premature EOF"; 34 case overflow => 35 return "Data does not fit into the encoder buffer"; 36 case let e: io::error => 37 return io::strerror(e); 38 }; 39 }; 40 41 fn wrap_err(e: error) io::error = { 42 match (e) { 43 case let e: io::error => 44 return e; 45 case let e: asn1error => 46 static assert(size(asn1error) <= size(errors::opaque_data)); 47 let w = errors::opaque_ { strerror = &wrap_strerror, ... }; 48 let ptr = &w.data: *error; 49 *ptr = e; 50 return w; 51 }; 52 }; 53 54 fn wrap_strerror(err: *errors::opaque_data) const str = { 55 let e = err: *error; 56 return strerror(*e); 57 }; 58 59 // Unwraps an [[io::error]] returned by ASN.1 readers as an [[error]]. 60 export fn unwrap_err(e: io::error) error = { 61 match (e) { 62 case let e: errors::opaque_ => 63 let ptr = &e.data: *error; 64 return *ptr; 65 case let e: io::error => 66 return e; 67 }; 68 };