From 57a01d5d38bbbc3a9c000f9409d66dcd53d4b40a Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 14 Mar 2017 18:19:58 +0200 Subject: [PATCH] Bug 18206: Use this feature for cities To test: 1. prove t/db_dependent/api/v1/cities.t Signed-off-by: Josef Moravec --- Koha/REST/V1/Cities.pm | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/Koha/REST/V1/Cities.pm b/Koha/REST/V1/Cities.pm index 8bca97cc65..032ce0acf3 100644 --- a/Koha/REST/V1/Cities.pm +++ b/Koha/REST/V1/Cities.pm @@ -18,6 +18,7 @@ package Koha::REST::V1::Cities; use Mojo::Base 'Mojolicious::Controller'; use Koha::Cities; +use Koha::Exceptions; use Try::Tiny; @@ -38,14 +39,7 @@ sub list { return $c->render( status => 200, openapi => $cities ); } catch { - if ( $_->isa('DBIx::Class::Exception') ) { - return $c->render( status => 500, - openapi => { error => $_->{msg} } ); - } - else { - return $c->render( status => 500, - openapi => { error => "Something went wrong, check the logs."} ); - } + Koha::Exceptions::rethrow_exception($_); }; } @@ -79,18 +73,7 @@ sub add { return $c->render( status => 200, openapi => _to_api($city->TO_JSON) ); } catch { - if ( $_->isa('DBIx::Class::Exception') ) { - return $c->render( - status => 500, - openapi => { error => $_->{msg} } - ); - } - else { - return $c->render( - status => 500, - openapi => { error => "Something went wrong, check the logs." } - ); - } + Koha::Exceptions::rethrow_exception($_); }; } @@ -115,14 +98,11 @@ sub update { return $c->render( status => 200, openapi => _to_api($city->TO_JSON) ); } catch { - if ( $_->isa('Koha::Exceptions::Object') ) { - return $c->render( status => 500, - openapi => { error => $_->message } ); - } - else { - return $c->render( status => 500, - openapi => { error => "Something went wrong, check the logs."} ); + if ( not defined $city ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); } + Koha::Exceptions::rethrow_exception($_); }; } @@ -144,14 +124,11 @@ sub delete { return $c->render( status => 200, openapi => "" ); } catch { - if ( $_->isa('DBIx::Class::Exception') ) { - return $c->render( status => 500, - openapi => { error => $_->{msg} } ); - } - else { - return $c->render( status => 500, - openapi => { error => "Something went wrong, check the logs."} ); + if ( not defined $city ) { + return $c->render( status => 404, + openapi => { error => "Object not found" } ); } + Koha::Exceptions::rethrow_exception($_); }; } -- 2.11.0