« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/App/Controller/WebBase.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Module.pm »
| 1 | package App::Controller::ApiBase; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Dwarf::Module::APIBase'; | ||
| 4 | use Dwarf::DSL; | ||
| 5 | use App::Constant; | ||
| 6 | use Class::Method::Modifiers; | ||
| 7 | |||
| 8 | # バリデーションエラー時に直ちにエラーを送出するかどうか | ||
| 9 | sub _build_autoflush_validation_error { 1 } | ||
| 10 | |||
| 11 | sub init_plugins { | ||
| 12 | load_plugins( | ||
| 13 | 'Error' => { | ||
| 14 | LACK_OF_PARAM => sub { shift->throw(1001, sprintf("missing mandatory parameters: %s", $_[0] || "")) }, | ||
| 15 | INVALID_PARAM => sub { shift->throw(1002, sprintf("illegal parameter: %s", $_[0] || "")) }, | ||
| 16 | INVALID_SESSION => sub { shift->throw(1003, sprintf("illegal session.")) }, | ||
| 17 | NEED_TO_LOGIN => sub { shift->throw(1004, sprintf("You must login.")) }, | ||
| 18 | SNS_LIMIT_ERROR => sub { shift->throw(2001, sprintf("SNS Limit Error: reset at %s", $_[0] || "")) }, | ||
| 19 | SNS_ERROR => sub { shift->throw(2002, sprintf("SNS Error: %s", $_[0] || "SNS Error.")) }, | ||
| 20 | ERROR => sub { shift->throw(9999, sprintf("%s", $_[0] || "Unknown Error.")) }, | ||
| 21 | }, | ||
| 22 | |||
| 23 | 'CGI::SpeedyCGI' => {}, | ||
| 24 | 'MouseX::Types::Common' => {}, | ||
| 25 | |||
| 26 | 'CORS' => { | ||
| 27 | origin => c->base_url, | ||
| 28 | credentials => 1, | ||
| 29 | headers => [qw/X-Requested-With Authorization Content-Type/], | ||
| 30 | maxage => 7200, | ||
| 31 | }, | ||
| 32 | |||
| 33 | 'JSON' => { | ||
| 34 | pretty => 1, | ||
| 35 | convert_blessed => 1, | ||
| 36 | }, | ||
| 37 | |||
| 38 | 'XML::Simple' => { | ||
| 39 | RootName => '<APP_NAME>', | ||
| 40 | NoAttr => 1, | ||
| 41 | KeyAttr => [], | ||
| 42 | SuppressEmpty => '' | ||
| 43 | }, | ||
| 44 | |||
| 45 | 'HTTP::Session' => { | ||
| 46 | session_key => conf('/session/state/name'), | ||
| 47 | session_table => conf('/session/store/table'), | ||
| 48 | session_expires => 60 * 60 * 24 * 21, | ||
| 49 | session_clean_thres => 0.001, | ||
| 50 | param_name => [qw/sessionId session_id/], | ||
| 51 | cookie_path => '/', | ||
| 52 | cookie_domain => undef, | ||
| 53 | cookie_expires => 60 * 60 * 24 * 21, | ||
| 54 | cookie_secure => conf('ssl') ? true : false, | ||
| 55 | cookie_httponly => 1, | ||
| 56 | }, | ||
| 57 | ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub will_dispatch { | ||
| 61 | } | ||
| 62 | |||
| 63 | sub did_dispatch { | ||
| 64 | } | ||
| 65 | |||
| 66 | after 'will_render' => sub { | ||
| 67 | my ($self, $c, $data) = @_; | ||
| 68 | return unless ref $data eq 'HASH'; | ||
| 69 | |||
| 70 | $data->{result} = 'success'; | ||
| 71 | |||
| 72 | if ($data->{error_code}) { | ||
| 73 | $data->{result} = "fail"; | ||
| 74 | } | ||
| 75 | }; | ||
| 76 | |||
| 77 | # 500 系のエラー | ||
| 78 | sub receive_server_error { | ||
| 79 | my ($self, $c, $error) = @_; | ||
| 80 | |||
| 81 | $error ||= 'Internal Server Error'; | ||
| 82 | print STDERR sprintf "[Server Error] %s\n", $error; | ||
| 83 | |||
| 84 | load_plugins('Devel::StackTrace' => {}); | ||
| 85 | |||
| 86 | # if (conf('/sentry/dsn')) { | ||
| 87 | # load_plugins(Sentry => { dsn => conf('/sentry/dsn') }); | ||
| 88 | # my %opt; | ||
| 89 | # # ユーザー情報を追加できる。 | ||
| 90 | # if (session) { | ||
| 91 | # $opt{user_context} = { username => ... }; | ||
| 92 | # } | ||
| 93 | # $c->call_sentry($error, %opt); | ||
| 94 | # } | ||
| 95 | |||
| 96 | my $data = { | ||
| 97 | error_code => 500, | ||
| 98 | error_message => $c->stacktrace($error), | ||
| 99 | }; | ||
| 100 | |||
| 101 | return $data; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; |