| 1 | 1 | ▶ | use v5.40; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use experimental 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use PAGI::Nano; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # The error model, all in one place. enable 'ErrorHandler' renders any uncaught | ||
| 6 | # exception as a tidy 500 (instead of the bare server default). For an error you | ||
| 7 | # *choose*, throw a respond-able: die $c->json(..., status => 4xx) and Nano | ||
| 8 | # sends it as-is — explicit, call-site-local, never a mystery die. not_found | ||
| 9 | # customizes the 404 for unmatched paths. | ||
| 10 | # | ||
| 11 | # pagi-server app.pl | ||
| 12 | # curl -i http://127.0.0.1:5000/ok # 200 | ||
| 13 | # curl -i http://127.0.0.1:5000/teapot # 418, chosen via die-a-respond-able | ||
| 14 | # curl -i http://127.0.0.1:5000/boom # 500, rendered by ErrorHandler | ||
| 15 | # curl -i http://127.0.0.1:5000/nowhere # 404, from not_found | ||
| 16 | |||
| 17 | 1 | ▶ | my $app = app { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 18 | 1 | enable 'ErrorHandler'; # uncaught exceptions -> a rendered 500 | |
| 19 | |||
| 20 | 1 | ▶ | get '/ok' => sub ($c) { { ok => 1 } }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | # An intentional, chosen error response: throw a respond-able value. | ||
| 23 | 1 | ▶ | get '/teapot' => sub ($c) { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 24 | 1 | die $c->json({ error => "I'm a teapot", hint => 'chosen on purpose' }, status => 418); | |
| 25 | 1 | }; | |
| 26 | |||
| 27 | # An unexpected failure: an ordinary die becomes a 500 (never a silent 200), | ||
| 28 | # and ErrorHandler renders it. | ||
| 29 | 1 | ▶ | get '/boom' => sub ($c) { die "something broke deep in the stack\n" }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | 1 | ▶ | not_found sub ($c) { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 32 | 1 | $c->json({ error => 'no such route', path => $c->req->path }, status => 404); | |
| 33 | 1 | }; | |
| 34 | 1 | }; | |
| 35 | |||
| 36 | 1 | $app; | |