« auto/share/dist/Zonemaster-Backend/patch/patch_sqlite_db_zonemaster_backend_ver_8.0.0.pl
↑ Summary
Zonemaster/Backend/DB/SQLite.pm »
| 1 | package Zonemaster::Backend::Error; | ||
| 2 | use Moose; | ||
| 3 | use Data::Dumper; | ||
| 4 | |||
| 5 | use overload '""' => \&as_string; | ||
| 6 | |||
| 7 | has 'message' => ( | ||
| 8 | is => 'ro', | ||
| 9 | isa => 'Str', | ||
| 10 | required => 1, | ||
| 11 | ); | ||
| 12 | |||
| 13 | has 'code' => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => 'Int', | ||
| 16 | required => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | has 'data' => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => 'Any', | ||
| 22 | default => undef, | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub as_hash { | ||
| 26 | my $self = shift; | ||
| 27 | my $error = { | ||
| 28 | code => $self->code, | ||
| 29 | message => $self->message, | ||
| 30 | error => ref($self), | ||
| 31 | }; | ||
| 32 | $error->{data} = $self->data if defined $self->data; | ||
| 33 | return $error; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub as_string { | ||
| 37 | my $self = shift; | ||
| 38 | my $str = sprintf "%s (code %d).", $self->message, $self->code; | ||
| 39 | if (defined $self->data) { | ||
| 40 | $str .= sprintf " Context: %s", $self->_data_dump; | ||
| 41 | } | ||
| 42 | return $str; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _data_dump { | ||
| 46 | my $self = shift; | ||
| 47 | local $Data::Dumper::Indent = 0; | ||
| 48 | local $Data::Dumper::Terse = 1; | ||
| 49 | my $data = Dumper($self->data); | ||
| 50 | $data =~ s/[\n\r]/ /g; | ||
| 51 | return $data ; | ||
| 52 | } | ||
| 53 | |||
| 54 | package Zonemaster::Backend::Error::Internal; | ||
| 55 | use Moose; | ||
| 56 | |||
| 57 | use overload '""' => \&as_string; | ||
| 58 | |||
| 59 | extends 'Zonemaster::Backend::Error'; | ||
| 60 | |||
| 61 | has '+message' => ( | ||
| 62 | default => 'Internal server error' | ||
| 63 | ); | ||
| 64 | |||
| 65 | has '+code' => ( | ||
| 66 | default => -32603 | ||
| 67 | ); | ||
| 68 | |||
| 69 | has 'reason' => ( | ||
| 70 | isa => 'Str', | ||
| 71 | is => 'ro' | ||
| 72 | ); | ||
| 73 | |||
| 74 | has 'method' => ( | ||
| 75 | is => 'ro', | ||
| 76 | isa => 'Str', | ||
| 77 | builder => '_build_method' | ||
| 78 | ); | ||
| 79 | |||
| 80 | sub _build_method { | ||
| 81 | my $s = 0; | ||
| 82 | while (my @c = caller($s)) { | ||
| 83 | $s ++; | ||
| 84 | last if $c[3] eq 'Moose::Object::new'; | ||
| 85 | } | ||
| 86 | my @c = caller($s); | ||
| 87 | if ($c[3] =~ /^(.*)::handle_exception$/ ) { | ||
| 88 | @c = caller(++$s); | ||
| 89 | } | ||
| 90 | |||
| 91 | return $c[3]; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub as_string { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | my $reason = $self->reason; | ||
| 98 | $reason =~ s/\s+/ /g; | ||
| 99 | $reason =~ s/^\s+|\s+$//g; | ||
| 100 | |||
| 101 | my $str = sprintf "Caught %s in the `%s` method: %s", ref($self), $self->method, $reason; | ||
| 102 | if (defined $self->data) { | ||
| 103 | $str .= sprintf " Context: %s", $self->_data_dump; | ||
| 104 | } | ||
| 105 | return $str; | ||
| 106 | } | ||
| 107 | |||
| 108 | around as_hash => sub { | ||
| 109 | my ($orig, $self) = @_; | ||
| 110 | |||
| 111 | my $hash = $self->$orig; | ||
| 112 | $hash->{reason} = $self->reason; | ||
| 113 | $hash->{method} = $self->method; | ||
| 114 | return $hash; | ||
| 115 | }; | ||
| 116 | |||
| 117 | |||
| 118 | package Zonemaster::Backend::Error::ResourceNotFound; | ||
| 119 | use Moose; | ||
| 120 | |||
| 121 | extends 'Zonemaster::Backend::Error'; | ||
| 122 | |||
| 123 | has '+message' => ( | ||
| 124 | default => 'Resource not found' | ||
| 125 | ); | ||
| 126 | |||
| 127 | has '+code' => ( | ||
| 128 | default => -32000 | ||
| 129 | ); | ||
| 130 | |||
| 131 | package Zonemaster::Backend::Error::PermissionDenied; | ||
| 132 | use Moose; | ||
| 133 | |||
| 134 | extends 'Zonemaster::Backend::Error'; | ||
| 135 | |||
| 136 | has '+message' => ( | ||
| 137 | default => 'Permission denied' | ||
| 138 | ); | ||
| 139 | |||
| 140 | has '+code' => ( | ||
| 141 | default => -32001 | ||
| 142 | ); | ||
| 143 | |||
| 144 | package Zonemaster::Backend::Error::Conflict; | ||
| 145 | use Moose; | ||
| 146 | |||
| 147 | extends 'Zonemaster::Backend::Error'; | ||
| 148 | |||
| 149 | has '+message' => ( | ||
| 150 | default => 'Conflicting resource' | ||
| 151 | ); | ||
| 152 | |||
| 153 | has '+code' => ( | ||
| 154 | default => -32002 | ||
| 155 | ); | ||
| 156 | |||
| 157 | package Zonemaster::Backend::Error::JsonError; | ||
| 158 | use Moose; | ||
| 159 | |||
| 160 | extends 'Zonemaster::Backend::Error::Internal'; | ||
| 161 | |||
| 162 | 1; |