« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/CGI/SpeedyCGI.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Validator/PlackRequest.pm »
| 1 | package Dwarf::Module::CLIBase; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module'; | ||
| 4 | use Dwarf::Validator; | ||
| 5 | |||
| 6 | use Dwarf::Accessor { | ||
| 7 | ro => [qw/autoflush_validation_error/], | ||
| 8 | }; | ||
| 9 | |||
| 10 | # バリデーションエラー時に直ちにエラーを送出するかどうか | ||
| 11 | sub _build_autoflush_validation_error { 1 } | ||
| 12 | |||
| 13 | sub init { | ||
| 14 | my ($self, $c) = @_; | ||
| 15 | $c->not_found if $c->is_production and not $c->is_cli; | ||
| 16 | |||
| 17 | $c->error->autoflush(1) if $self->autoflush_validation_error; | ||
| 18 | |||
| 19 | $c->load_plugins( | ||
| 20 | 'Error' => { | ||
| 21 | LACK_OF_PARAM => sub { shift->throw(1001, sprintf("missing mandatory parameters: %s", $_[0] || "")) }, | ||
| 22 | INVALID_PARAM => sub { shift->throw(1002, sprintf("illegal parameter: %s", $_[0] || "")) }, | ||
| 23 | SNS_LIMIT_ERROR => sub { shift->throw(2001, sprintf("SNS Limit Error: reset at %s", $_[0] || "")) }, | ||
| 24 | SNS_ERROR => sub { shift->throw(2002, sprintf("SNS Error: %s", $_[0] || "SNS Error.")) }, | ||
| 25 | ERROR => sub { shift->throw(400, sprintf("%s", $_[0] || "Unknown Error.")) }, | ||
| 26 | }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | $c->add_trigger(BEFORE_RENDER => $self->can('will_render')); | ||
| 30 | $c->add_trigger(AFTER_RENDER => $self->can('did_render')); | ||
| 31 | $c->add_trigger(ERROR => $self->can('receive_error')); | ||
| 32 | $c->add_trigger(SERVER_ERROR => $self->can('receive_server_error')); | ||
| 33 | |||
| 34 | $self->type('text/plain; charset=UTF-8'); | ||
| 35 | |||
| 36 | $self->init_plugins($c); | ||
| 37 | $self->call_before_trigger($c); | ||
| 38 | $self->will_dispatch($c); | ||
| 39 | $self->error->flush; | ||
| 40 | $self->error->autoflush(1); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub will_dispatch {} | ||
| 44 | sub did_dispatch {} | ||
| 45 | |||
| 46 | sub init_plugins { | ||
| 47 | my ($self, $c) = @_; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub call_before_trigger { | ||
| 51 | my ($self, $c) = @_; | ||
| 52 | $c->call_trigger(BEFORE_DISPATCH => $c, $c->request); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub validate { | ||
| 56 | my ($self, @rules) = @_; | ||
| 57 | return unless @rules; | ||
| 58 | |||
| 59 | my $validator = Dwarf::Validator->new($self->c->req)->check(@rules); | ||
| 60 | if ($validator->has_error) { | ||
| 61 | while (my ($param, $detail) = each %{ $validator->errors }) { | ||
| 62 | $self->c->error->LACK_OF_PARAM($param) if $detail->{NOT_NULL}; | ||
| 63 | $self->c->error->INVALID_PARAM($param); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | # レンダリング前の共通処理 | ||
| 69 | sub will_render { | ||
| 70 | my ($self, $c, $data) = @_; | ||
| 71 | } | ||
| 72 | |||
| 73 | # レンダリング後の共通処理 | ||
| 74 | sub did_render { | ||
| 75 | my ($self, $c, $data) = @_; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub receive_error { | ||
| 79 | my ($self, $c, $error) = @_; | ||
| 80 | return $error; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub receive_server_error { | ||
| 84 | my ($self, $c, $error) = @_; | ||
| 85 | print STDERR sprintf "[Server Error] %s\n", $error; | ||
| 86 | return $error; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 |