Curie/Component/MainWindow/Role/ExceptionHandler.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod n/a totaltotal 0.0% 0 / 50 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use Renard::Incunabula::Common::Setup;
2 package Renard::Curie::Component::MainWindow::Role::ExceptionHandler;
3 # ABSTRACT: A role for handling exceptions
4 $Renard::Curie::Component::MainWindow::Role::ExceptionHandler::VERSION = '0.005';
5 use Moo::Role;
6 use Gtk3;
7 use Glib 'TRUE', 'FALSE';
8  
9 use constant EXCEPTION_DIALOG_LABEL_MAX_WIDTH_CHARS => 30;
10  
11 has _exception_handler => (
12 is => 'lazy', # _build__exception_handler
13 );
14  
15 method _build__exception_handler() {
16 Glib->install_exception_handler( my $cb = fun( $exception ) {
17 if ( $exception->isa('Renard::Curie::Error::User::InvalidPageNumber') ) {
18 $self->_show_exception_dialog_box(
19 'Page number entered is invalid: entered "%s", should be between %s and %s.',
20 [
21 $exception->payload->{text},
22 $exception->payload->{range}[0],
23 $exception->payload->{range}[1],
24 ],
25 );
26 } else {
27 $self->_show_exception_dialog_box( "$exception" );
28 }
29  
30 return TRUE; # keep the handler
31 });
32 };
33  
34  
35 method _show_exception_dialog_box( $message_fmt, $params = [] ) {
36 my $flags = [ qw/modal destroy_with_parent/ ];
37 my $dialog = Gtk3::Dialog->new(
38 "Error",
39 $self->window,
40 $flags,
41 "_Close", 'close',
42 );
43  
44 my $hbox = Gtk3::Box->new( 'horizontal', 0 );
45  
46 # add image
47 $hbox->add(
48 Gtk3::Image->new_from_icon_name ("dialog-error", 'dialog')
49 );
50  
51 # add label
52 my $label = Gtk3::Label->new(
53 sprintf($message_fmt, @$params)
54 );
55 $label->set_line_wrap(TRUE);
56 $label->set_max_width_chars(EXCEPTION_DIALOG_LABEL_MAX_WIDTH_CHARS);
57 $label->set_selectable(TRUE);
58 $hbox->pack_end( $label, TRUE, FALSE, 0 );
59  
60 $dialog->get_content_area->pack_start( $hbox, TRUE, TRUE, 0 );
61  
62 $dialog->signal_connect( response => sub { $dialog->destroy } );
63  
64 $dialog->show_all;
65  
66 my $result = $dialog->run;
67 }
68  
69 after setup_window => method() {
70 $self->_exception_handler;
71 };
72  
73 1;
74  
75 __END__