-
Notifications
You must be signed in to change notification settings - Fork 24
Add custom error handling #847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hannaeko
merged 17 commits into
zonemaster:develop
from
hannaeko:add-custom-error-handling
Sep 7, 2021
Merged
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
af3b173
add errors module
hannaeko 1462532
improve errors
hannaeko 6484d04
change log level
hannaeko f9d0d8d
fix comment
hannaeko 5f551c6
remove unrelated file
hannaeko febf070
improve code
hannaeko c5378a6
add more error types
hannaeko 2cc1bf8
remove error id
hannaeko b7716d5
use error classes instead of strings
hannaeko e73d74f
Merge remote-tracking branch 'origin/develop' into add-custom-error-h…
hannaeko 5f9ee3e
remove specialized as_hash method for internal error
hannaeko 9d22bef
remove unused variable
hannaeko 9dc0b3d
improve code formatting
hannaeko 169019f
improve code formatting
hannaeko b32c1b9
make reason read only
hannaeko 1c803df
remove method name from handle_exception calls
hannaeko c50e8b4
update manifest
hannaeko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package Zonemaster::Backend::Error; | ||
| use Moose; | ||
| use Data::Dumper; | ||
|
|
||
|
|
||
| has 'message' => ( | ||
| is => 'rw', | ||
hannaeko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| isa => 'Str', | ||
| required => 1, | ||
| ); | ||
|
|
||
| has 'code' => ( | ||
| is => 'rw', | ||
| isa => 'Int', | ||
| required => 1, | ||
| ); | ||
|
|
||
| has 'data' => ( | ||
| is => 'rw', | ||
| isa => 'Any', | ||
| default => undef, | ||
| ); | ||
|
|
||
| sub as_hash { | ||
| my $self = shift; | ||
| my $error = { | ||
| code => $self->code, | ||
| message => $self->message, | ||
| error => ref($self), | ||
| }; | ||
| $error->{data} = $self->data if defined $self->data; | ||
| return $error; | ||
| } | ||
|
|
||
| sub as_string { | ||
| my $self = shift; | ||
| my $str = sprintf "%s (code %d)", $self->message, $self->code; | ||
| if (defined $self->data) { | ||
| $str .= sprintf "; Context: %s", $self->_data_dump; | ||
| } | ||
| return $str; | ||
| } | ||
|
|
||
| sub _data_dump { | ||
| my $self = shift; | ||
| local $Data::Dumper::Indent = 0; | ||
| local $Data::Dumper::Terse = 1; | ||
| my $data = Dumper($self->data); | ||
| $data =~ s/[\n\r]/ /g; | ||
| return $data ; | ||
| } | ||
|
|
||
| package Zonemaster::Backend::Error::Internal; | ||
| use Moose; | ||
|
|
||
| extends 'Zonemaster::Backend::Error'; | ||
|
|
||
| has '+message' => ( | ||
| default => 'Internal server error' | ||
| ); | ||
|
|
||
| has '+code' => ( | ||
| default => -32603 | ||
| ); | ||
|
|
||
| has 'reason' => ( | ||
| isa => 'Str', | ||
| is => 'rw', | ||
mattias-p marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| initializer => 'reason', | ||
| ); | ||
|
|
||
| has 'method' => ( | ||
| is => 'rw', | ||
| isa => 'Str', | ||
| builder => '_build_method' | ||
| ); | ||
|
|
||
| has 'id' => ( | ||
| is => 'rw', | ||
| isa => 'Int', | ||
| default => 0, | ||
| ); | ||
|
|
||
| sub _build_method { | ||
| my $s = 0; | ||
| while (my @c = caller($s)) { | ||
| $s ++; | ||
| last if $c[3] eq 'Moose::Object::new'; | ||
| } | ||
| my @c = caller($s); | ||
| if ($c[3] =~ /^(.*)::handle_exception$/ ) { | ||
| @c = caller(++$s); | ||
| } | ||
|
|
||
| return $c[3]; | ||
| } | ||
|
|
||
| around 'reason' => sub { | ||
| my $orig = shift; | ||
| my $self = shift; | ||
|
|
||
| my ( $value, $setter, $attr ) = @_; | ||
|
|
||
| # reader | ||
| return $self->$orig if not $value; | ||
|
|
||
| # trim new lines | ||
| $value =~ s/\n/ /g; | ||
| $value =~ s/^\s+|\s+$//g; | ||
|
|
||
| # initializer | ||
| return $setter->($value) if $setter; | ||
|
|
||
| # writer | ||
| $self->$orig($value); | ||
| }; | ||
|
|
||
| around 'as_hash' => sub { | ||
| my $orig = shift; | ||
| my $self = shift; | ||
|
|
||
| my $href = $self->$orig; | ||
|
|
||
| $href->{exception_id} = $self->id; | ||
| $href->{reason} = $self->reason; | ||
| $href->{method} = $self->method; | ||
mattias-p marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return $href; | ||
| }; | ||
|
|
||
|
|
||
| sub as_string { | ||
| my $self = shift; | ||
| my $str = sprintf "Internal error %0.3d (%s): Unexpected error in the `%s` method: [%s]", $self->id, ref($self), $self->method, $self->reason; | ||
| if (defined $self->data) { | ||
| $str .= sprintf "; Context: %s", $self->_data_dump; | ||
| } | ||
| return $str; | ||
| } | ||
|
|
||
|
|
||
| package Zonemaster::Backend::Error::ResourceNotFound; | ||
| use Moose; | ||
|
|
||
| extends 'Zonemaster::Backend::Error'; | ||
|
|
||
| has '+message' => ( | ||
| default => 'Resource not found' | ||
| ); | ||
|
|
||
| has '+code' => ( | ||
| default => -32000 | ||
| ); | ||
|
|
||
| package Zonemaster::Backend::Error::JsonError; | ||
| use Moose; | ||
|
|
||
| extends 'Zonemaster::Backend::Error::Internal'; | ||
|
|
||
| 1; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #!/usr/bin/env perl | ||
| use strict; | ||
| use warnings; | ||
|
|
||
| use Zonemaster::Backend::Config; | ||
| use Zonemaster::Backend::DB; | ||
| use Net::Prometheus; | ||
|
|
||
| my $client = Net::Prometheus->new; | ||
|
|
||
| my $tests_gauge = $client->new_gauge( | ||
| name => 'zonemaster_tests_total', | ||
| help => 'Total number of tests', | ||
| labels => [ 'state' ], | ||
| ); | ||
|
|
||
| my $config = Zonemaster::Backend::Config->load_config(); | ||
| my $dbtype = $config->DB_engine; | ||
| my $dbclass = Zonemaster::Backend::DB->get_db_class( $dbtype ); | ||
| my $db = $dbclass->from_config( $config ); | ||
|
|
||
| my $prom_app = $client->psgi_app; | ||
|
|
||
| sub { | ||
| my $queued = $db->dbh->selectrow_hashref('SELECT count(*) from test_results WHERE progress = 0'); | ||
| $tests_gauge->labels('queued')->set($queued->{count}); | ||
|
|
||
| my $finished = $db->dbh->selectrow_hashref('SELECT count(*) from test_results WHERE progress = 100'); | ||
| $tests_gauge->labels('finished')->set($finished->{count}); | ||
|
|
||
| my $running = $db->dbh->selectrow_hashref('SELECT count(*) from test_results WHERE progress > 0 and progress < 100'); | ||
| $tests_gauge->labels('running')->set($running->{count}); | ||
|
|
||
| $prom_app->(@_); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.