File: Error.pm

package info (click to toggle)
libzonemaster-perl 8.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,880 kB
  • sloc: perl: 18,014; makefile: 16
file content (147 lines) | stat: -rw-r--r-- 2,955 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package Zonemaster::Engine::Normalization::Error;

use v5.16.0;
use warnings;

use Carp;
use Readonly;
use Locale::TextDomain qw[Zonemaster-Engine];

use overload '""' => \&string;


=head1 NAME

Zonemaster::Engine::Normalization::Error - normalization error class


=head1 SYNOPSIS

    use Zonemaster::Engine::Normalization::Error;

    my $error = Zonemaster::Engine::Normalization::Error->new(LABEL_TOO_LONG => {label => $label});

=cut


Readonly my %ERRORS => (
    AMBIGUOUS_DOWNCASING => {
        message => N__ 'Ambiguous downcasing of character "{unicode_name}" in the domain name. Use all lower case instead.',
        arguments => [ 'unicode_name' ]
    },
    DOMAIN_NAME_TOO_LONG => {
        message => N__ 'Domain name is too long (more than 253 characters with no final dot).',
    },
    EMPTY_DOMAIN_NAME => {
        message => N__ 'Domain name is empty.'
    },
    INITIAL_DOT => {
        message => N__ 'Domain name starts with dot.'
    },
    INVALID_ASCII => {
        message => N__ 'Domain name has an ASCII label ("{label}") with a character not permitted.',
        arguments => [ 'label' ]
    },
    INVALID_U_LABEL => {
        message => N__ 'Domain name has a non-ASCII label ("{label}") which is not a valid U-label.',
        arguments => [ 'label' ]
    },
    REPEATED_DOTS => {
        message => N__ 'Domain name has repeated dots.'
    },
    LABEL_TOO_LONG => {
        message => N__ 'Domain name has a label that is too long (more than 63 characters), "{label}".',
        arguments => [ 'label' ]
    },
);

=head1 ATTRIBUTES

=over

=item tag

The message tag associated to the error.

=item params

The error message parameters to use in the message string.

=back

=head1 METHODS

=over

=item new($tag, $params)

Creates and returns a new error object.
This function will croak if there is a missing parameter for the given tag.

=cut

sub new {
    my ( $proto, $tag, $params ) = @_;
    my $class = ref $proto || $proto;

    if ( !exists $ERRORS{$tag} ) {
        croak 'Unknown error tag.';
    }

    my $obj = { tag => $tag, params => {} };

    if ( exists $ERRORS{$tag}->{arguments} ) {
        foreach my $arg ( @{$ERRORS{$tag}->{arguments}} ) {
            if ( !exists $params->{$arg} ) {
                croak "Missing arguments $arg.";
            }
            $obj->{params}->{$arg} = $params->{$arg};
        }
    }

    return bless $obj, $class;
}


=item message

Returns the translated error message using the parameters given when creating the object.

=cut

sub message {
    my ( $self ) = @_;
    return __x $ERRORS{$self->{tag}}->{message}, %{$self->{params}};
}


=item tag

Returns the message tag associated to the error.

=cut

sub tag {
    my ( $self ) = @_;

    return $self->{tag};
}

=item string

Returns a string representation of the error object. Equivalent to message().

=cut

sub string {
    my ( $self ) = @_;

    return $self->message;
}


=back

=cut

1;