hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

error.ha (2312B)


      1 // License: MPL-2.0
      2 // (c) 2021 Drew DeVault <sir@cmpwn.com>
      3 // (c) 2021 Eyal Sawady <ecs@d2evs.net>
      4 use errors;
      5 use fmt;
      6 use net;
      7 
      8 // The DNS message was poorly formatted.
      9 export type format = !void;
     10 
     11 // The name server was unable to process this query due to a problem with the
     12 // name server.
     13 export type server_failure = !void;
     14 
     15 // The domain name referenced in the query does not exist. Meaningful only for
     16 // responses from an authoritative name server.
     17 export type name_error = !void;
     18 
     19 // The name server does not support the requested kind of query.
     20 export type not_implemented = !void;
     21 
     22 // The name server refuses to perform the specified operation for policy
     23 // reasons.
     24 export type refused = !void;
     25 
     26 // Any other server-provided error condition not known to Hare.
     27 export type unknown_error = !u8;
     28 
     29 // All error types which might be returned from [[net::dns]] functions.
     30 export type error = !(format | server_failure | name_error
     31 	| not_implemented | refused | unknown_error
     32 	| errors::overflow | errors::timeout | net::error);
     33 
     34 export fn strerror(err: error) const str = {
     35 	static let buf: [64]u8 = [0...];
     36 	match (err) {
     37 	case format =>
     38 		return "The DNS message was poorly formatted";
     39 	case server_failure =>
     40 		return "The name server was unable to process this query due to a problem with the name server";
     41 	case name_error =>
     42 		return "The domain name referenced in the query does not exist";
     43 	case not_implemented =>
     44 		return "The name server does not support the requested kind of query";
     45 	case refused =>
     46 		return "The name server refuses to perform the specified operation for policy reasons";
     47 	case let ue: unknown_error =>
     48 		return fmt::bsprintf(buf, "Unknown DNS error {}", ue: u8);
     49 	case errors::overflow =>
     50 		return "The encoded message would exceed the buffer size";
     51 	case errors::timeout =>
     52 		return "The DNS request timed out";
     53 	case let err: net::error =>
     54 		return net::strerror(err);
     55 	};
     56 };
     57 
     58 fn check_rcode(rcode: rcode) (void | error) = {
     59 	switch (rcode) {
     60 	case rcode::NO_ERROR => void;
     61 	case rcode::FMT_ERROR =>
     62 		return format;
     63 	case rcode::SERVER_FAILURE =>
     64 		return server_failure;
     65 	case rcode::NAME_ERROR =>
     66 		return name_error;
     67 	case rcode::NOT_IMPLEMENTED =>
     68 		return not_implemented;
     69 	case rcode::REFUSED =>
     70 		return refused;
     71 	case =>
     72 		return rcode: unknown_error;
     73 	};
     74 };