hare

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

error.ha (4417B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use fmt;
      6 use io;
      7 use net;
      8 
      9 // The DNS message was poorly formatted.
     10 export type format = !void;
     11 
     12 // The name server was unable to process this query due to a problem with the
     13 // name server.
     14 export type server_failure = !void;
     15 
     16 // The domain name referenced in the query does not exist. Meaningful only for
     17 // responses from an authoritative name server.
     18 export type name_error = !void;
     19 
     20 // The name server does not support the requested kind of query.
     21 export type not_implemented = !void;
     22 
     23 // The name server refuses to perform the specified operation for policy
     24 // reasons.
     25 export type refused = !void;
     26 
     27 // Dynamic update prerequisite unsatisfied: a domain name exists when it
     28 // shouldn't.
     29 export type name_exists = !void;
     30 
     31 // Dynamic update prerequisite unsatisfied: a resource record set exists when it
     32 // shouldn't.
     33 export type rrset_exists = !void;
     34 
     35 // Dynamic update prerequisite unsatisfied: a resource record set doesn't exists
     36 // when it should.
     37 export type rrset_error = !void;
     38 
     39 // Server not authoritative for the zone or request not authorized.
     40 export type not_auth = !void;
     41 
     42 // Name not contained in zone.
     43 export type not_zone = !void;
     44 
     45 // TSIG signature validation failed.
     46 export type bad_sig = !void;
     47 
     48 // Key not recognized.
     49 export type bad_key = !void;
     50 
     51 // Signature out of time window.
     52 export type bad_time = !void;
     53 
     54 // Any other server-provided error condition not known to Hare.
     55 export type unknown_error = !u8;
     56 
     57 // All error types which might be returned from functions in this module.
     58 export type error = !(format | server_failure | name_error
     59 	| not_implemented | refused | name_exists
     60 	| rrset_exists | rrset_error | not_auth | not_zone
     61 	| bad_sig | bad_key | bad_time | unknown_error
     62 	| errors::invalid | errors::overflow | errors::timeout
     63 	| net::error | io::error);
     64 
     65 // Converts an error into a human-friendly string. The result may be statically
     66 // allocated.
     67 export fn strerror(err: error) const str = {
     68 	static let buf: [64]u8 = [0...];
     69 	match (err) {
     70 	case format =>
     71 		return "The DNS message was poorly formatted";
     72 	case server_failure =>
     73 		return "The name server was unable to process this query due to a problem with the name server";
     74 	case name_error =>
     75 		return "The domain name referenced in the query does not exist";
     76 	case not_implemented =>
     77 		return "The name server does not support the requested kind of query";
     78 	case refused =>
     79 		return "The name server refuses to perform the specified operation for policy reasons";
     80 	case name_exists =>
     81 		return "Dynamic update prerequisite unsatisfied: a domain name exists when it shouldn't";
     82 	case rrset_exists =>
     83 		return "Dynamic update prerequisite unsatisfied: a resource record set exists when it shouldn't";
     84 	case rrset_error =>
     85 		return "Dynamic update prerequisite unsatisfied: a resource record set doesn't exist when it should";
     86 	case not_auth =>
     87 		return "Server not authoritative for the zone or request not authorized";
     88 	case not_zone =>
     89 		return "Name not contained in zone";
     90 	case bad_sig =>
     91 		return "TSIG signature validation failed";
     92 	case bad_key =>
     93 		return "Key not recognized";
     94 	case bad_time =>
     95 		return "Signature out of time window";
     96 	case let ue: unknown_error =>
     97 		return fmt::bsprintf(buf, "Unknown DNS error {}", ue: u8);
     98 	case errors::invalid =>
     99 		return "The message contains one or more field with invalid values";
    100 	case errors::overflow =>
    101 		return "The encoded message would exceed the buffer size";
    102 	case errors::timeout =>
    103 		return "The DNS request timed out";
    104 	case let err: net::error =>
    105 		return net::strerror(err);
    106 	case let err: io::error =>
    107 		return io::strerror(err);
    108 	};
    109 };
    110 
    111 fn check_rcode(rcode: rcode) (void | error) = {
    112 	switch (rcode) {
    113 	case rcode::NOERROR => void;
    114 	case rcode::FORMERR =>
    115 		return format;
    116 	case rcode::SERVFAIL =>
    117 		return server_failure;
    118 	case rcode::NXDOMAIN =>
    119 		return name_error;
    120 	case rcode::NOTIMP =>
    121 		return not_implemented;
    122 	case rcode::REFUSED =>
    123 		return refused;
    124 	case rcode::YXDOMAIN =>
    125 		return name_exists;
    126 	case rcode::YXRRSET =>
    127 		return rrset_exists;
    128 	case rcode::NXRRSET =>
    129 		return rrset_error;
    130 	case rcode::NOTAUTH =>
    131 		return not_auth;
    132 	case rcode::NOTZONE =>
    133 		return not_zone;
    134 	case rcode::BADSIG =>
    135 		return bad_sig;
    136 	case rcode::BADKEY =>
    137 		return bad_key;
    138 	case rcode::BADTIME =>
    139 		return bad_time;
    140 	case =>
    141 		return rcode: unknown_error;
    142 	};
    143 };