From: Damyan Ivanov Date: Tue, 15 Feb 2022 21:12:29 +0000 (+0000) Subject: return exception responses as JSON X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=941f3fd80a4e9c96821a5e50f05bb04cf356304c;p=lsl.git return exception responses as JSON --- diff --git a/lib/App/LazyShoppingList/API.pm b/lib/App/LazyShoppingList/API.pm index c828974..84e7fad 100644 --- a/lib/App/LazyShoppingList/API.pm +++ b/lib/App/LazyShoppingList/API.pm @@ -9,7 +9,7 @@ use Exporter qw(import); our $VERSION = '0.1'; our @EXPORT = qw( &get_database &get_lists_version &increment_lists_version - &invalid_input ); + &invalid_input exception ); use Dancer2::Plugin::DBIC; @@ -66,8 +66,13 @@ sub increment_lists_version($dbh) { } sub invalid_input($details = undef) { - status 400; - return "Invalid input" . ( defined($details) ? " ($details)" : '' ); + return exception 400, + "Invalid input" . ( defined($details) ? " ($details)" : '' ); +} + +sub exception($http_code, $text) { + status $http_code; + return { exception => $text }; } diff --git a/lib/App/LazyShoppingList/API/v1.pm b/lib/App/LazyShoppingList/API/v1.pm index 8a30acb..f94db2e 100644 --- a/lib/App/LazyShoppingList/API/v1.pm +++ b/lib/App/LazyShoppingList/API/v1.pm @@ -13,9 +13,7 @@ use JSON(); use experimental 'signatures'; set charset => 'UTF-8'; -set serializer => 'JSON'; -set content_type => 'application/json'; - +set serializer => 'JSON'; # get the URI for the list of shopping lists get '/' => sub { @@ -48,9 +46,7 @@ post '/list' => sub { my $name = $req->{name}; unless ($name) { - status 400; - content_type 'text/plain'; - return "Missing list name"; + return exception 400, "Missing list name"; } my $dbh = get_database; @@ -87,9 +83,7 @@ get '/list/:list_id' => sub { unless ($list) { $dbh->txn_commit; - status 404; - content_type 'text/plain'; - return "No list with that ID found"; + return exception 404, "No list with that ID found"; } $r{version} = $list->version; @@ -133,9 +127,7 @@ post '/list/:id' => sub { unless ($list) { $dbh->txn_commit; - status 404; - content_type 'text/plain'; - return "No such list"; + exception 404, "No such list"; } my $item = $dbh->resultset('ShoppingListItem')->create( @@ -186,17 +178,13 @@ put '/list/:list_id/:item_id' => sub { my $list = $dbh->resultset('ShoppingList')->find($list_id); unless ($list) { $dbh->txn_commit; - status 404; - content_type 'text/plain'; - return "No such list"; + return exception 404, "No such list"; } my $item = $dbh->resultset('ShoppingListItem')->find({shopping_list => $list->id, id => $item_id}); unless ($item) { $dbh->txn_commit; - status 404; - content_type 'text/plain'; - return "No such item"; + return exception 404, "No such item"; } # in case no real changes are needed will return the current state @@ -205,9 +193,7 @@ put '/list/:list_id/:item_id' => sub { { unless ($version == $item->version) { $dbh->txn_commit; - status 409; - content_type 'text/plain'; - return + return exception 409, exception => sprintf( 'Outdated version (current is %d)', $item->version ); }