| 1 | package Zonemaster::Engine::Normalization::Error; | ||
| 2 | |||
| 3 | use v5.16.0; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Readonly; | ||
| 8 | use Locale::TextDomain qw[Zonemaster-Engine]; | ||
| 9 | |||
| 10 | use overload '""' => \&string; | ||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Zonemaster::Engine::Normalization::Error - normalization error class | ||
| 16 | |||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Zonemaster::Engine::Normalization::Error; | ||
| 21 | |||
| 22 | my $error = Zonemaster::Engine::Normalization::Error->new(LABEL_TOO_LONG => {label => $label}); | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | |||
| 27 | Readonly my %ERRORS => ( | ||
| 28 | AMBIGUOUS_DOWNCASING => { | ||
| 29 | message => N__ 'Ambiguous downcasing of character "{unicode_name}" in the domain name. Use all lower case instead.', | ||
| 30 | arguments => [ 'unicode_name' ] | ||
| 31 | }, | ||
| 32 | DOMAIN_NAME_TOO_LONG => { | ||
| 33 | message => N__ 'Domain name is too long (more than 253 characters with no final dot).', | ||
| 34 | }, | ||
| 35 | EMPTY_DOMAIN_NAME => { | ||
| 36 | message => N__ 'Domain name is empty.' | ||
| 37 | }, | ||
| 38 | INITIAL_DOT => { | ||
| 39 | message => N__ 'Domain name starts with dot.' | ||
| 40 | }, | ||
| 41 | INVALID_ASCII => { | ||
| 42 | message => N__ 'Domain name has an ASCII label ("{label}") with a character not permitted.', | ||
| 43 | arguments => [ 'label' ] | ||
| 44 | }, | ||
| 45 | INVALID_U_LABEL => { | ||
| 46 | message => N__ 'Domain name has a non-ASCII label ("{label}") which is not a valid U-label.', | ||
| 47 | arguments => [ 'label' ] | ||
| 48 | }, | ||
| 49 | REPEATED_DOTS => { | ||
| 50 | message => N__ 'Domain name has repeated dots.' | ||
| 51 | }, | ||
| 52 | LABEL_TOO_LONG => { | ||
| 53 | message => N__ 'Domain name has a label that is too long (more than 63 characters), "{label}".', | ||
| 54 | arguments => [ 'label' ] | ||
| 55 | }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | =head1 ATTRIBUTES | ||
| 59 | |||
| 60 | =over | ||
| 61 | |||
| 62 | =item tag | ||
| 63 | |||
| 64 | The message tag associated to the error. | ||
| 65 | |||
| 66 | =item params | ||
| 67 | |||
| 68 | The error message parameters to use in the message string. | ||
| 69 | |||
| 70 | =back | ||
| 71 | |||
| 72 | =head1 METHODS | ||
| 73 | |||
| 74 | =over | ||
| 75 | |||
| 76 | =item new($tag, $params) | ||
| 77 | |||
| 78 | Creates and returns a new error object. | ||
| 79 | This function will croak if there is a missing parameter for the given tag. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub new { | ||
| 84 | my ( $proto, $tag, $params ) = @_; | ||
| 85 | my $class = ref $proto || $proto; | ||
| 86 | |||
| 87 | if ( !exists $ERRORS{$tag} ) { | ||
| 88 | croak 'Unknown error tag.'; | ||
| 89 | } | ||
| 90 | |||
| 91 | my $obj = { tag => $tag, params => {} }; | ||
| 92 | |||
| 93 | if ( exists $ERRORS{$tag}->{arguments} ) { | ||
| 94 | foreach my $arg ( @{$ERRORS{$tag}->{arguments}} ) { | ||
| 95 | if ( !exists $params->{$arg} ) { | ||
| 96 | croak "Missing arguments $arg."; | ||
| 97 | } | ||
| 98 | $obj->{params}->{$arg} = $params->{$arg}; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | return bless $obj, $class; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | =item message | ||
| 107 | |||
| 108 | Returns the translated error message using the parameters given when creating the object. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub message { | ||
| 113 | my ( $self ) = @_; | ||
| 114 | return __x $ERRORS{$self->{tag}}->{message}, %{$self->{params}}; | ||
| 115 | } | ||
| 116 | |||
| 117 | |||
| 118 | =item tag | ||
| 119 | |||
| 120 | Returns the message tag associated to the error. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub tag { | ||
| 125 | my ( $self ) = @_; | ||
| 126 | |||
| 127 | return $self->{tag}; | ||
| 128 | } | ||
| 129 | |||
| 130 | =item string | ||
| 131 | |||
| 132 | Returns a string representation of the error object. Equivalent to message(). | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub string { | ||
| 137 | my ( $self ) = @_; | ||
| 138 | |||
| 139 | return $self->message; | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | 1; |