From f52a06402b4067a8d4c20e6e18764632f409279c Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 2 Apr 2024 18:22:52 +0000 Subject: [PATCH] Bug 36495: Use resource_not_found() helper everywhere This patch makes the existing controllers use the new helpers for (most) 404 situations. The remaining ones are subject to discussion. I proposed to discuss on the next meeting, how to make this more consistent. For reference: ```shell git grep resource_not_found ``` and see the different use cases in the codebase. Once we settle on the matter, I will provide a follow-up to make them all the same. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/api/v1/ => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/REST/V1/Acquisitions/Orders.pm | 21 +--- Koha/REST/V1/Acquisitions/Vendor/Issues.pm | 8 +- Koha/REST/V1/Acquisitions/Vendors.pm | 34 ++---- Koha/REST/V1/AdvancedEditorMacro.pm | 45 ++++---- Koha/REST/V1/ArticleRequests.pm | 24 +--- .../REST/V1/Auth/Identity/Provider/Domains.pm | 55 ++------- Koha/REST/V1/Auth/Identity/Providers.pm | 34 ++---- Koha/REST/V1/AuthorisedValues.pm | 8 +- Koha/REST/V1/Authorities.pm | 27 ++--- Koha/REST/V1/BackgroundJobs.pm | 6 +- Koha/REST/V1/Biblios.pm | 105 +++++------------- Koha/REST/V1/Biblios/ItemGroups.pm | 8 +- Koha/REST/V1/Biblios/ItemGroups/Items.pm | 10 +- Koha/REST/V1/Bookings.pm | 25 ++--- Koha/REST/V1/CashRegisters/Cashups.pm | 9 +- Koha/REST/V1/Checkouts.pm | 44 +++----- Koha/REST/V1/Cities.pm | 24 ++-- Koha/REST/V1/Clubs/Holds.pm | 10 +- Koha/REST/V1/Config/SMTP/Servers.pm | 26 +---- Koha/REST/V1/ERM/Agreements.pm | 25 ++--- Koha/REST/V1/ERM/Documents.pm | 8 +- Koha/REST/V1/ERM/EHoldings/Packages/Local.pm | 25 ++--- Koha/REST/V1/ERM/EHoldings/Resources/Local.pm | 8 +- Koha/REST/V1/ERM/EHoldings/Titles/Local.pm | 30 ++--- Koha/REST/V1/ERM/EUsage/CounterFiles.pm | 16 +-- .../REST/V1/ERM/EUsage/DefaultUsageReports.pm | 9 +- Koha/REST/V1/ERM/EUsage/UsageDataProviders.pm | 42 ++----- Koha/REST/V1/ERM/Licenses.pm | 25 ++--- Koha/REST/V1/Holds.pm | 48 +++----- Koha/REST/V1/IllbatchStatuses.pm | 21 +--- Koha/REST/V1/Illbatches.pm | 29 ++--- Koha/REST/V1/ImportBatchProfiles.pm | 15 +-- Koha/REST/V1/ImportRecordMatches.pm | 18 +-- Koha/REST/V1/Items.pm | 67 ++++------- Koha/REST/V1/Libraries.pm | 37 ++---- Koha/REST/V1/Patrons.pm | 24 +--- Koha/REST/V1/Patrons/Account.pm | 40 ++----- Koha/REST/V1/Patrons/Attributes.pm | 70 +++--------- Koha/REST/V1/Patrons/Holds.pm | 15 +-- Koha/REST/V1/Patrons/Password.pm | 5 +- Koha/REST/V1/Patrons/Password/Expiration.pm | 5 +- Koha/REST/V1/Preservation/Processings.pm | 25 ++--- Koha/REST/V1/Preservation/Trains.pm | 97 +++++----------- Koha/REST/V1/Preservation/WaitingList.pm | 8 +- Koha/REST/V1/Quotes.pm | 22 ++-- Koha/REST/V1/ReturnClaims.pm | 26 ++--- Koha/REST/V1/SearchFilter.pm | 20 ++-- Koha/REST/V1/Suggestions.pm | 20 +--- Koha/REST/V1/Tickets.pm | 34 ++---- t/db_dependent/api/v1/article_requests.t | 6 +- t/db_dependent/api/v1/authorities.t | 2 +- t/db_dependent/api/v1/biblios.t | 4 +- t/db_dependent/api/v1/erm_eholdings_titles.t | 7 +- t/db_dependent/api/v1/patrons.t | 5 +- t/db_dependent/api/v1/suggestions.t | 2 +- 55 files changed, 383 insertions(+), 1000 deletions(-) diff --git a/Koha/REST/V1/Acquisitions/Orders.pm b/Koha/REST/V1/Acquisitions/Orders.pm index b254e4da7e7..c4d6f8430cd 100644 --- a/Koha/REST/V1/Acquisitions/Orders.pm +++ b/Koha/REST/V1/Acquisitions/Orders.pm @@ -97,12 +97,8 @@ sub get { my $order = Koha::Acquisition::Orders->find( $c->param('order_id') ); - unless ($order) { - return $c->render( - status => 404, - openapi => { error => "Order not found" } - ); - } + return $c->resource_not_found("Order") + unless $order; return try { return $c->render( @@ -160,12 +156,8 @@ sub update { my $order = Koha::Acquisition::Orders->find( $c->param('order_id') ); - unless ($order) { - return $c->render( - status => 404, - openapi => { error => "Order not found" } - ); - } + return $c->resource_not_found("Order") + unless $order; return try { $order->set_from_api( $c->req->json ); @@ -195,10 +187,7 @@ sub delete { my $order = Koha::Acquisition::Orders->find( $c->param('order_id') ); unless ($order) { - return $c->render( - status => 404, - openapi => { error => 'Order not found' } - ); + return $c->resource_not_found("Order"); } elsif ( ( $order->orderstatus && $order->orderstatus ne 'cancelled' ) || !$order->datecancellationprinted ) { # Koha may (historically) have inconsistent order data here (e.g. cancelled without date) return $c->render( diff --git a/Koha/REST/V1/Acquisitions/Vendor/Issues.pm b/Koha/REST/V1/Acquisitions/Vendor/Issues.pm index 79a9e4a8238..2914b4a13ad 100644 --- a/Koha/REST/V1/Acquisitions/Vendor/Issues.pm +++ b/Koha/REST/V1/Acquisitions/Vendor/Issues.pm @@ -48,12 +48,8 @@ sub list { my $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') ); - unless ($vendor) { - return $c->render( - status => 404, - openapi => { error => "Vendor not found." } - ); - } + return $c->resource_not_found("Vendor") + unless $vendor; my $issues_rs = $vendor->issues; my $issues = $c->objects->search($issues_rs); diff --git a/Koha/REST/V1/Acquisitions/Vendors.pm b/Koha/REST/V1/Acquisitions/Vendors.pm index d36801b74d9..81d84bd1243 100644 --- a/Koha/REST/V1/Acquisitions/Vendors.pm +++ b/Koha/REST/V1/Acquisitions/Vendors.pm @@ -63,12 +63,9 @@ sub get { my $c = shift->openapi->valid_input or return; my $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') ); - unless ($vendor) { - return $c->render( - status => 404, - openapi => { error => "Vendor not found" } - ); - } + + return $c->resource_not_found("Vendor") + unless $vendor; return try { return $c->render( @@ -114,25 +111,20 @@ Controller function that handles updating a Koha::Acquisition::Bookseller object sub update { my $c = shift->openapi->valid_input or return; - my $vendor; + my $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') ); + + return $c->resource_not_found("Object") + unless $vendor; return try { - $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') ); $vendor->set_from_api( $c->req->json ); $vendor->store(); + return $c->render( status => 200, openapi => $c->objects->to_api($vendor), ); - } - catch { - if ( not defined $vendor ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } - + } catch { $c->unhandled_exception($_); }; @@ -150,12 +142,8 @@ sub delete { return try { my $vendor = Koha::Acquisition::Booksellers->find( $c->param('vendor_id') ); - unless ( $vendor ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $vendor; $vendor->delete; diff --git a/Koha/REST/V1/AdvancedEditorMacro.pm b/Koha/REST/V1/AdvancedEditorMacro.pm index 1ac1c7806e4..0f53a2a4d1c 100644 --- a/Koha/REST/V1/AdvancedEditorMacro.pm +++ b/Koha/REST/V1/AdvancedEditorMacro.pm @@ -67,12 +67,10 @@ sub get { my $c = shift->openapi->valid_input or return; my $patron = $c->stash('koha.user'); my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); - unless ($macro) { - return $c->render( - status => 404, - openapi => { error => "Macro not found" } - ); - } + + return $c->resource_not_found("Macro") + unless $macro; + if( $macro->shared ){ return $c->render( status => 403, openapi => { error => "This macro is shared, you must access it via advanced_editor/macros/shared" @@ -99,10 +97,10 @@ sub get_shared { my $macro = Koha::AdvancedEditorMacros->find({ id => $c->param('advancededitormacro_id'), }); - unless ($macro) { - return $c->render( status => 404, - openapi => { error => "Macro not found" } ); - } + + return $c->resource_not_found("Macro") + unless $macro; + unless( $macro->shared ){ return $c->render( status => 403, openapi => { error => "This macro is not shared, you must access it via advanced_editor/macros" @@ -181,10 +179,9 @@ sub update { my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); - if ( not defined $macro ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found + unless $macro; + my $patron = $c->stash('koha.user'); my $body = $c->req->json; @@ -222,10 +219,8 @@ sub update_shared { my $body = $c->req->json; - if ( not defined $macro ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found + unless $macro; unless( $macro->shared == 1 || defined $body->{shared} && $body->{shared} == 1 ){ return $c->render( status => 403, @@ -252,10 +247,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); - if ( not defined $macro ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + + return $c->resource_not_found + unless $macro; my $patron = $c->stash('koha.user'); if( $macro->shared == 1 ){ @@ -287,10 +281,9 @@ sub delete_shared { my $c = shift->openapi->valid_input or return; my $macro = Koha::AdvancedEditorMacros->find( $c->param('advancededitormacro_id') ); - if ( not defined $macro ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + + return $c->resource_not_found + unless $macro; unless( $macro->shared == 1 ){ return $c->render( status => 403, diff --git a/Koha/REST/V1/ArticleRequests.pm b/Koha/REST/V1/ArticleRequests.pm index 4419bca673e..3996a234ce1 100644 --- a/Koha/REST/V1/ArticleRequests.pm +++ b/Koha/REST/V1/ArticleRequests.pm @@ -44,12 +44,8 @@ sub cancel { my $article_request = Koha::ArticleRequests->find( $c->param('article_request_id') ); - unless ( $article_request ) { - return $c->render( - status => 404, - openapi => { error => "Article request not found" } - ); - } + return $c->resource_not_found("Article request") + unless $article_request; my $reason = $c->param('cancellation_reason'); my $notes = $c->param('notes'); @@ -82,23 +78,15 @@ sub patron_cancel { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ( $patron ) { - return $c->render( - status => 404, - openapi => { error => "Patron not found" } - ); - } + return $c->resource_not_found("Patron") + unless $patron; # patron_id has been validated by the allow-owner check, so the following call to related # article requests covers the case of article requests not belonging to the patron my $article_request = $patron->article_requests->find( $c->param('article_request_id') ); - unless ( $article_request ) { - return $c->render( - status => 404, - openapi => { error => "Article request not found" } - ); - } + return $c->resource_not_found("Article request") + unless $article_request; my $reason = $c->param('cancellation_reason'); my $notes = $c->param('notes'); diff --git a/Koha/REST/V1/Auth/Identity/Provider/Domains.pm b/Koha/REST/V1/Auth/Identity/Provider/Domains.pm index de7be5557b8..7a840298caf 100644 --- a/Koha/REST/V1/Auth/Identity/Provider/Domains.pm +++ b/Koha/REST/V1/Auth/Identity/Provider/Domains.pm @@ -46,15 +46,8 @@ sub list { return try { my $provider = Koha::Auth::Identity::Providers->find( $c->param('identity_provider_id') ); - unless ($provider) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $provider; my $domains_rs = $provider->domains; return $c->render( @@ -79,29 +72,15 @@ sub get { my $provider = Koha::Auth::Identity::Providers->find( $c->param('identity_provider_id') ); - unless ($provider) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $provider; my $domains_rs = $provider->domains; my $domain = $c->objects->find( $domains_rs, $c->param('identity_provider_domain_id') ); - unless ($domain) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $domain; return $c->render( status => 200, openapi => $domain ); } catch { @@ -164,15 +143,8 @@ sub update { } ); - unless ($domain) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $domain; return try { @@ -209,15 +181,8 @@ sub delete { } ); - unless ($domain) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $domain; return try { $domain->delete; diff --git a/Koha/REST/V1/Auth/Identity/Providers.pm b/Koha/REST/V1/Auth/Identity/Providers.pm index 33dc6d18dad..258a155c6eb 100644 --- a/Koha/REST/V1/Auth/Identity/Providers.pm +++ b/Koha/REST/V1/Auth/Identity/Providers.pm @@ -68,15 +68,8 @@ sub get { my $provider = $c->objects->find( Koha::Auth::Identity::Providers->new, $c->param('identity_provider_id') ); - unless ( $provider ) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $provider; return $c->render( status => 200, openapi => $provider ); } @@ -148,15 +141,8 @@ sub update { my $provider = Koha::Auth::Identity::Providers->find( $c->param('identity_provider_id') ); - unless ( $provider ) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + return $c->resource_not_found("Object") + unless $provider; return try { @@ -210,15 +196,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $provider = Koha::Auth::Identity::Providers->find( $c->param('identity_provider_id') ); - unless ( $provider ) { - return $c->render( - status => 404, - openapi => { - error => 'Object not found', - error_code => 'not_found', - } - ); - } + + return $c->resource_not_found("Object") + unless $provider; return try { $provider->delete; diff --git a/Koha/REST/V1/AuthorisedValues.pm b/Koha/REST/V1/AuthorisedValues.pm index 3a4706f6681..b7d6a452664 100644 --- a/Koha/REST/V1/AuthorisedValues.pm +++ b/Koha/REST/V1/AuthorisedValues.pm @@ -38,12 +38,8 @@ sub list_av_from_category { my $category = Koha::AuthorisedValueCategories->find($c->param('authorised_value_category_name')); - unless ($category) { - return $c->render( - status => 404, - openapi => { error => "Category not found" } - ); - } + return $c->resource_not_found("Category") + unless $category; return try { my $av_set = $category->authorised_values->search_with_library_limits; diff --git a/Koha/REST/V1/Authorities.pm b/Koha/REST/V1/Authorities.pm index bfc15f0e69b..ed5b8b00b88 100644 --- a/Koha/REST/V1/Authorities.pm +++ b/Koha/REST/V1/Authorities.pm @@ -41,14 +41,9 @@ sub get { my $c = shift->openapi->valid_input or return; my $authority = Koha::Authorities->find( { authid => $c->param('authority_id') } ); - unless ( $authority ) { - return $c->render( - status => 404, - openapi => { - error => "Object not found." - } - ); - } + + return $c->resource_not_found("Object") + unless $authority; return try { @@ -111,12 +106,8 @@ sub delete { my $authority = Koha::Authorities->find( { authid => $c->param('authority_id') } ); - if ( not defined $authority ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $authority; return try { DelAuthority( { authid => $authority->authid } ); @@ -206,12 +197,8 @@ sub update { my $authid = $c->param('authority_id'); my $authority = Koha::Authorities->find( { authid => $authid } ); - if ( not defined $authority ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $authority; try { my $headers = $c->req->headers; diff --git a/Koha/REST/V1/BackgroundJobs.pm b/Koha/REST/V1/BackgroundJobs.pm index f56397db686..f93184a971b 100644 --- a/Koha/REST/V1/BackgroundJobs.pm +++ b/Koha/REST/V1/BackgroundJobs.pm @@ -75,10 +75,8 @@ sub get { my $job = Koha::BackgroundJobs->find($job_id); - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ) unless $job; + return $c->resource_not_found("Object") + unless $job; return $c->render( status => 403, diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index 49b449c3c1e..e6ee64cd557 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -56,14 +56,8 @@ sub get { my $biblio = Koha::Biblios->find( { biblionumber => $c->param('biblio_id') }, $attributes ); - unless ( $biblio ) { - return $c->render( - status => 404, - openapi => { - error => "Object not found." - } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { @@ -128,12 +122,8 @@ sub delete { my $biblio = Koha::Biblios->find( $c->param('biblio_id') ); - if ( not defined $biblio ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { my $error = DelBiblio( $biblio->id ); @@ -166,14 +156,8 @@ sub get_public { { biblionumber => $c->param('biblio_id') }, { prefetch => ['metadata'] } ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { - error => "Object not found." - } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { @@ -187,14 +171,8 @@ sub get_public { # unless there's a logged in user, and there's an exception for it's # category unless ( $patron and $patron->category->override_hidden_items ) { - if ( $biblio->hidden_in_opac({ rules => $opachiddenitems_rules }) ) - { - return $c->render( - status => 404, - openapi => { - error => "Object not found." - } - ); + if ( $biblio->hidden_in_opac( { rules => $opachiddenitems_rules } ) ) { + return $c->resource_not_found("Object"); } } @@ -259,12 +237,8 @@ sub get_bookings { my $biblio = Koha::Biblios->find( { biblionumber => $c->param('biblio_id') }, { prefetch => ['bookings'] } ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Object not found." } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { @@ -291,12 +265,8 @@ sub get_items { my $biblio = Koha::Biblios->find( { biblionumber => $c->param('biblio_id') }, { prefetch => ['items'] } ); my $bookable_only = $c->param('bookable'); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Object not found." } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { @@ -325,12 +295,8 @@ sub add_item { my $biblio_id = $c->param('biblio_id'); my $biblio = Koha::Biblios->find( $biblio_id ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Biblio not found" } - ); - } + return $c->resource_not_found("Object") + unless $biblio; my $body = $c->req->json; @@ -438,21 +404,14 @@ sub update_item { my $biblio_id = $c->param('biblio_id'); my $item_id = $c->param('item_id'); my $biblio = Koha::Biblios->find( { biblionumber => $biblio_id } ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Biblio not found" } - ); - } + + return $c->resource_not_found("Biblio") + unless $biblio; my $item = $biblio->items->find({ itemnumber => $item_id }); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; my $body = $c->req->json; @@ -496,12 +455,8 @@ sub get_checkouts { try { my $biblio = Koha::Biblios->find( $c->param('biblio_id') ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => 'Object not found' } - ); - } + return $c->resource_not_found("Object") + unless $biblio; my $checkouts = ($checked_in) @@ -530,12 +485,8 @@ sub pickup_locations { my $biblio = Koha::Biblios->find( $c->param('biblio_id') ); - unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Biblio not found" } - ); - } + return $c->resource_not_found("Biblio") + unless $biblio; my $patron = Koha::Patrons->find( $c->param('patron_id') ); $c->req->params->remove('patron_id'); @@ -599,14 +550,8 @@ sub get_items_public { { prefetch => ['items'] } ); - unless ( $biblio ) { - return $c->render( - status => 404, - openapi => { - error => "Object not found." - } - ); - } + return $c->resource_not_found("Object") + unless $biblio; return try { diff --git a/Koha/REST/V1/Biblios/ItemGroups.pm b/Koha/REST/V1/Biblios/ItemGroups.pm index 8dc4a16c1a1..4de16091716 100644 --- a/Koha/REST/V1/Biblios/ItemGroups.pm +++ b/Koha/REST/V1/Biblios/ItemGroups.pm @@ -206,12 +206,8 @@ sub delete { } ); - if ( not defined $item_group ) { - return $c->render( - status => 404, - openapi => { error => "Item group not found" } - ); - } + return $c->resource_not_found("Item group") + unless $item_group; return try { $item_group->delete; diff --git a/Koha/REST/V1/Biblios/ItemGroups/Items.pm b/Koha/REST/V1/Biblios/ItemGroups/Items.pm index 10d3f021ead..4e6561377c9 100644 --- a/Koha/REST/V1/Biblios/ItemGroups/Items.pm +++ b/Koha/REST/V1/Biblios/ItemGroups/Items.pm @@ -45,14 +45,8 @@ sub add { my $item_group = Koha::Biblio::ItemGroups->find( $c->param('item_group_id') ); - unless ( $item_group ) { - return $c->render( - status => 404, - openapi => { - error => 'Item group not found' - } - ); - } + return $c->resource_not_found("Item group") + unless $item_group; unless ( $item_group->biblio_id eq $c->param('biblio_id') ) { return $c->render( diff --git a/Koha/REST/V1/Bookings.pm b/Koha/REST/V1/Bookings.pm index 2d1bb25ad83..a4c142e0270 100644 --- a/Koha/REST/V1/Bookings.pm +++ b/Koha/REST/V1/Bookings.pm @@ -55,12 +55,9 @@ sub get { return try { my $booking = $c->objects->find( Koha::Bookings->new, $c->param('booking_id') ); - unless ($booking) { - return $c->render( - status => 404, - openapi => { error => "Booking not found" } - ); - } + + return $c->resource_not_found("Booking") + unless $booking; return $c->render( status => 200, openapi => $booking ); } catch { @@ -115,12 +112,8 @@ sub update { my $booking = $c->objects->find_rs( Koha::Bookings->new, $c->param('booking_id') ); - if ( not defined $booking ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $booking; return try { $booking->set_from_api( $c->req->json ); @@ -142,12 +135,8 @@ sub delete { my $booking = Koha::Bookings->find( $c->param('booking_id') ); - unless ( $booking ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $booking; return try { $booking->delete; diff --git a/Koha/REST/V1/CashRegisters/Cashups.pm b/Koha/REST/V1/CashRegisters/Cashups.pm index 25aa4abf879..e1ddffdc11e 100644 --- a/Koha/REST/V1/CashRegisters/Cashups.pm +++ b/Koha/REST/V1/CashRegisters/Cashups.pm @@ -71,12 +71,9 @@ sub get { return try { my $cashup = Koha::Cash::Register::Cashups->find( $c->param('cashup_id') ); - unless ($cashup) { - return $c->render( - status => 404, - openapi => { error => "Cashup not found" } - ); - } + + return $c->resource_not_found("Cashup") + unless $cashup; return $c->render( status => 200, diff --git a/Koha/REST/V1/Checkouts.pm b/Koha/REST/V1/Checkouts.pm index db3bee3c268..276937e9ece 100644 --- a/Koha/REST/V1/Checkouts.pm +++ b/Koha/REST/V1/Checkouts.pm @@ -79,16 +79,13 @@ sub get { my $c = shift->openapi->valid_input or return; my $checkout_id = $c->param('checkout_id'); - my $checkout = Koha::Checkouts->find( $checkout_id ); - $checkout = Koha::Old::Checkouts->find( $checkout_id ) - unless ($checkout); - unless ($checkout) { - return $c->render( - status => 404, - openapi => { error => "Checkout doesn't exist" } - ); - } + my $checkout = Koha::Checkouts->find($checkout_id); + $checkout = Koha::Old::Checkouts->find($checkout_id) + unless $checkout; + + return $c->resource_not_found("Checkout") + unless $checkout; return try { return $c->render( @@ -295,16 +292,13 @@ sub get_renewals { try { my $checkout_id = $c->param('checkout_id'); - my $checkout = Koha::Checkouts->find($checkout_id); + + my $checkout = Koha::Checkouts->find($checkout_id); $checkout = Koha::Old::Checkouts->find($checkout_id) - unless ($checkout); + unless $checkout; - unless ($checkout) { - return $c->render( - status => 404, - openapi => { error => "Checkout doesn't exist" } - ); - } + return $c->resource_not_found("Checkout") + unless $checkout; my $renewals_rs = $checkout->renewals; my $renewals = $c->objects->search( $renewals_rs ); @@ -333,12 +327,8 @@ sub renew { my $seen = $c->param('seen') || 1; my $checkout = Koha::Checkouts->find( $checkout_id ); - unless ($checkout) { - return $c->render( - status => 404, - openapi => { error => "Checkout doesn't exist" } - ); - } + return $c->resource_not_found("Checkout") + unless $checkout; return try { my ($can_renew, $error) = CanBookBeRenewed($checkout->patron, $checkout); @@ -383,12 +373,8 @@ sub allows_renewal { my $checkout_id = $c->param('checkout_id'); my $checkout = Koha::Checkouts->find( $checkout_id ); - unless ($checkout) { - return $c->render( - status => 404, - openapi => { error => "Checkout doesn't exist" } - ); - } + return $c->resource_not_found("Checkout") + unless $checkout; return try { my ($can_renew, $error) = CanBookBeRenewed($checkout->patron, $checkout); diff --git a/Koha/REST/V1/Cities.pm b/Koha/REST/V1/Cities.pm index a13c9680d75..f3b54aaff31 100644 --- a/Koha/REST/V1/Cities.pm +++ b/Koha/REST/V1/Cities.pm @@ -53,16 +53,13 @@ sub get { return try { my $city = Koha::Cities->find( $c->param('city_id') ); - unless ($city) { - return $c->render( status => 404, - openapi => { error => "City not found" } ); - } + return $c->resource_not_found("City") + unless $city; return $c->render( status => 200, openapi => $c->objects->to_api($city), ); - } - catch { + } catch { $c->unhandled_exception($_); - } + }; } =head3 add @@ -95,10 +92,8 @@ sub update { my $city = Koha::Cities->find( $c->param('city_id') ); - if ( not defined $city ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found + unless $city; return try { $city->set_from_api( $c->req->json ); @@ -118,10 +113,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $city = Koha::Cities->find( $c->param('city_id') ); - if ( not defined $city ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + + return $c->resource_not_found + unless $city; return try { $city->delete; diff --git a/Koha/REST/V1/Clubs/Holds.pm b/Koha/REST/V1/Clubs/Holds.pm index d202657f99b..be868b5abba 100644 --- a/Koha/REST/V1/Clubs/Holds.pm +++ b/Koha/REST/V1/Clubs/Holds.pm @@ -76,10 +76,7 @@ sub add { my $item = Koha::Items->find($item_id); unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); + return $c->resource_not_found("Item"); } else { $biblio = $item->biblio; @@ -96,10 +93,7 @@ sub add { } unless ($biblio) { - return $c->render( - status => 404, - openapi => { error => "Biblio not found" } - ); + return $c->resource_not_found("Biblio"); } my $club_hold = Koha::Club::Hold::add( diff --git a/Koha/REST/V1/Config/SMTP/Servers.pm b/Koha/REST/V1/Config/SMTP/Servers.pm index 09422134a33..26085b385ad 100644 --- a/Koha/REST/V1/Config/SMTP/Servers.pm +++ b/Koha/REST/V1/Config/SMTP/Servers.pm @@ -61,14 +61,8 @@ sub get { return try { my $smtp_server = Koha::SMTP::Servers->find( $c->param('smtp_server_id') ); - unless ($smtp_server) { - return $c->render( - status => 404, - openapi => { - error => "SMTP server not found" - } - ); - } + return $c->resource_not_found("SMTP server") + unless $smtp_server; return $c->render( status => 200, @@ -127,14 +121,8 @@ sub update { my $smtp_server = Koha::SMTP::Servers->find( $c->param('smtp_server_id') ); - if ( not defined $smtp_server ) { - return $c->render( - status => 404, - openapi => { - error => "Object not found" - } - ); - } + return $c->resource_not_found("Object") + unless $smtp_server; return try { $smtp_server->set_from_api( $c->req->json ); @@ -171,10 +159,8 @@ sub delete { my $smtp_server = Koha::SMTP::Servers->find( $c->param('smtp_server_id') ); - if ( not defined $smtp_server ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found("Object") + unless $smtp_server; return try { $smtp_server->delete; diff --git a/Koha/REST/V1/ERM/Agreements.pm b/Koha/REST/V1/ERM/Agreements.pm index 71096762ed2..c75921373a7 100644 --- a/Koha/REST/V1/ERM/Agreements.pm +++ b/Koha/REST/V1/ERM/Agreements.pm @@ -64,12 +64,8 @@ sub get { return try { my $agreement = $c->objects->find( Koha::ERM::Agreements->search, $c->param('agreement_id') ); - unless ($agreement) { - return $c->render( - status => 404, - openapi => { error => "Agreement not found" } - ); - } + return $c->resource_not_found("Agreement") + unless $agreement; return $c->render( status => 200, @@ -171,12 +167,8 @@ sub update { my $agreement = Koha::ERM::Agreements->find( $c->param('agreement_id') ); - unless ($agreement) { - return $c->render( - status => 404, - openapi => { error => "Agreement not found" } - ); - } + return $c->resource_not_found("Agreement") + unless $agreement; return try { Koha::Database->new->schema->txn_do( @@ -249,12 +241,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $agreement = Koha::ERM::Agreements->find( $c->param('agreement_id') ); - unless ($agreement) { - return $c->render( - status => 404, - openapi => { error => "Agreement not found" } - ); - } + + return $c->resource_not_found("Agreement") + unless $agreement; return try { $agreement->delete; diff --git a/Koha/REST/V1/ERM/Documents.pm b/Koha/REST/V1/ERM/Documents.pm index a1d824ce95d..3f7005e58e0 100644 --- a/Koha/REST/V1/ERM/Documents.pm +++ b/Koha/REST/V1/ERM/Documents.pm @@ -42,12 +42,8 @@ sub get { # Do not use $c->objects->find here, we need the file_content my $document = Koha::ERM::Documents->find( $c->param('document_id') ); - if ( !$document ) { - return $c->render( - status => 404, - openapi => { error => "Document not found" } - ); - } + return $c->resource_not_found("Document") + unless $document; $c->render_file('data' => $document->file_content, 'filename' => $document->file_name); } diff --git a/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm b/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm index 7d966ce891e..e42c8961ed5 100644 --- a/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm +++ b/Koha/REST/V1/ERM/EHoldings/Packages/Local.pm @@ -59,12 +59,8 @@ sub get { $package_id ); - unless ($package) { - return $c->render( - status => 404, - openapi => { error => "Package not found" } - ); - } + return $c->resource_not_found("Package") + unless $package; return $c->render( status => 200, @@ -154,12 +150,8 @@ sub update { my $package_id = $c->param('package_id'); my $package = Koha::ERM::EHoldings::Packages->find( $package_id ); - unless ($package) { - return $c->render( - status => 404, - openapi => { error => "Package not found" } - ); - } + return $c->resource_not_found("Package") + unless $package; return try { Koha::Database->new->schema->txn_do( @@ -222,12 +214,9 @@ sub delete { my $c = shift or return; my $package = Koha::ERM::EHoldings::Packages->find( $c->param('package_id') ); - unless ($package) { - return $c->render( - status => 404, - openapi => { error => "Package not found" } - ); - } + + return $c->resource_not_found("Package") + unless $package; return try { $package->delete; diff --git a/Koha/REST/V1/ERM/EHoldings/Resources/Local.pm b/Koha/REST/V1/ERM/EHoldings/Resources/Local.pm index 96323c339c1..30ce932b291 100644 --- a/Koha/REST/V1/ERM/EHoldings/Resources/Local.pm +++ b/Koha/REST/V1/ERM/EHoldings/Resources/Local.pm @@ -63,12 +63,8 @@ sub get { my $resource_id = $c->param('resource_id'); my $resource = $c->objects->find( Koha::ERM::EHoldings::Resources->search, $resource_id ); - unless ($resource ) { - return $c->render( - status => 404, - openapi => { error => "eHolding resource not found" } - ); - } + return $c->resource_not_found("eHolding resource") + unless $resource; return $c->render( status => 200, diff --git a/Koha/REST/V1/ERM/EHoldings/Titles/Local.pm b/Koha/REST/V1/ERM/EHoldings/Titles/Local.pm index eddf02f4102..37b030420bd 100644 --- a/Koha/REST/V1/ERM/EHoldings/Titles/Local.pm +++ b/Koha/REST/V1/ERM/EHoldings/Titles/Local.pm @@ -59,12 +59,8 @@ sub get { return try { my $title = $c->objects->find( Koha::ERM::EHoldings::Titles->search, $c->param('title_id') ); - unless ($title ) { - return $c->render( - status => 404, - openapi => { error => "eHolding title not found" } - ); - } + return $c->resource_not_found("eHolding title") + unless $title; return $c->render( status => 200, @@ -153,12 +149,8 @@ sub update { my $title = Koha::ERM::EHoldings::Titles->find( $c->param('title_id') ); - unless ($title) { - return $c->render( - status => 404, - openapi => { error => "eHolding title not found" } - ); - } + return $c->resource_not_found("eHolding title") + unless $title; return try { Koha::Database->new->schema->txn_do( @@ -218,12 +210,9 @@ sub delete { my $c = shift or return; my $title = Koha::ERM::EHoldings::Titles->find( $c->param('title_id') ); - unless ($title) { - return $c->render( - status => 404, - openapi => { error => "eHolding title not found" } - ); - } + + return $c->resource_not_found("eHolding title") + unless $title; return try { $title->delete; @@ -252,10 +241,7 @@ sub import_from_list { my $patron = $c->stash('koha.user'); unless ( $list && $list->owner == $c->stash('koha.user')->borrowernumber ) { - return $c->render( - status => 404, - openapi => { error => "List not found" } - ); + return $c->resource_not_found("List"); } diff --git a/Koha/REST/V1/ERM/EUsage/CounterFiles.pm b/Koha/REST/V1/ERM/EUsage/CounterFiles.pm index ca65d6fabce..daff3527ed2 100644 --- a/Koha/REST/V1/ERM/EUsage/CounterFiles.pm +++ b/Koha/REST/V1/ERM/EUsage/CounterFiles.pm @@ -59,12 +59,8 @@ sub get { return try { my $counter_file = Koha::ERM::EUsage::CounterFiles->find( $c->param('erm_counter_files_id') ); - if ( !$counter_file ) { - return $c->render( - status => 404, - openapi => { error => "Counter file not found" } - ); - } + return $c->resource_not_found("Counter file") + unless $counter_file; $c->render_file( 'data' => $counter_file->file_content, @@ -84,12 +80,8 @@ sub delete { my $counter_file = Koha::ERM::EUsage::CounterFiles->find( $c->param('erm_counter_files_id') ); - unless ($counter_file) { - return $c->render( - status => 404, - openapi => { error => "Counter file not found" } - ); - } + return $c->resource_not_found("Counter file") + unless $counter_file; return try { $counter_file->delete; diff --git a/Koha/REST/V1/ERM/EUsage/DefaultUsageReports.pm b/Koha/REST/V1/ERM/EUsage/DefaultUsageReports.pm index 4fa760673a4..861ead9d5f0 100644 --- a/Koha/REST/V1/ERM/EUsage/DefaultUsageReports.pm +++ b/Koha/REST/V1/ERM/EUsage/DefaultUsageReports.pm @@ -113,12 +113,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $default_report = Koha::ERM::EUsage::DefaultUsageReports->find( $c->param('erm_default_usage_report_id') ); - unless ($default_report) { - return $c->render( - status => 404, - openapi => { error => "Default report not found" } - ); - } + + return $c->resource_not_found("Default report") + unless $default_report; return try { $default_report->delete; diff --git a/Koha/REST/V1/ERM/EUsage/UsageDataProviders.pm b/Koha/REST/V1/ERM/EUsage/UsageDataProviders.pm index 193bf43d1a2..2326643d30e 100644 --- a/Koha/REST/V1/ERM/EUsage/UsageDataProviders.pm +++ b/Koha/REST/V1/ERM/EUsage/UsageDataProviders.pm @@ -124,12 +124,8 @@ sub get { my $usage_data_provider = Koha::ERM::EUsage::UsageDataProviders->find( $c->param('erm_usage_data_provider_id') ); - unless ($usage_data_provider) { - return $c->render( - status => 404, - openapi => { error => "Usage data provider not found" } - ); - } + return $c->resource_not_found("Usage data provider") + unless $usage_data_provider; return $c->render( status => 200, @@ -208,12 +204,8 @@ sub update { my $usage_data_provider = Koha::ERM::EUsage::UsageDataProviders->find( $c->param('erm_usage_data_provider_id') ); - unless ($usage_data_provider) { - return $c->render( - status => 404, - openapi => { error => "Usage data provider not found" } - ); - } + return $c->resource_not_found("Usage data provider") + unless $usage_data_provider; return try { Koha::Database->new->schema->txn_do( @@ -265,12 +257,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $usage_data_provider = Koha::ERM::EUsage::UsageDataProviders->find( $c->param('erm_usage_data_provider_id') ); - unless ($usage_data_provider) { - return $c->render( - status => 404, - openapi => { error => "Usage data provider not found" } - ); - } + + return $c->resource_not_found("Usage data provider") + unless $usage_data_provider; return try { $usage_data_provider->delete; @@ -388,12 +377,8 @@ sub process_SUSHI_response { my $udprovider = Koha::ERM::EUsage::UsageDataProviders->find( $c->param('erm_usage_data_provider_id') ); - unless ($udprovider) { - return $c->render( - status => 404, - openapi => { error => "Usage data provider not found" } - ); - } + return $c->resource_not_found("Usage data provider") + unless $udprovider; return try { my $jobs = $udprovider->enqueue_sushi_harvest_jobs( @@ -421,12 +406,9 @@ sub test_connection { my $udprovider = Koha::ERM::EUsage::UsageDataProviders->find( $c->param('erm_usage_data_provider_id') ); - unless ($udprovider) { - return $c->render( - status => 404, - openapi => { error => "Usage data provider not found" } - ); - } + return $c->resource_not_found("Usage data provider") + unless $udprovider; + try { my $service_active = $udprovider->test_connection; return $c->render( diff --git a/Koha/REST/V1/ERM/Licenses.pm b/Koha/REST/V1/ERM/Licenses.pm index 4e15d703cee..8cd977e3409 100644 --- a/Koha/REST/V1/ERM/Licenses.pm +++ b/Koha/REST/V1/ERM/Licenses.pm @@ -57,12 +57,8 @@ sub get { return try { my $license = $c->objects->find( Koha::ERM::Licenses->search, $c->param('license_id') ); - unless ($license) { - return $c->render( - status => 404, - openapi => { error => "License not found" } - ); - } + return $c->resource_not_found("License") + unless $license; return $c->render( status => 200, @@ -158,12 +154,8 @@ sub update { my $license = Koha::ERM::Licenses->find( $c->param('license_id') ); - unless ($license) { - return $c->render( - status => 404, - openapi => { error => "License not found" } - ); - } + return $c->resource_not_found("License") + unless $license; return try { Koha::Database->new->schema->txn_do( @@ -230,12 +222,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $license = Koha::ERM::Licenses->find( $c->param('license_id') ); - unless ($license) { - return $c->render( - status => 404, - openapi => { error => "License not found" } - ); - } + + return $c->resource_not_found("License") + unless $license; return try { $license->delete; diff --git a/Koha/REST/V1/Holds.pm b/Koha/REST/V1/Holds.pm index e34d23199ba..037eb3fb6e9 100644 --- a/Koha/REST/V1/Holds.pm +++ b/Koha/REST/V1/Holds.pm @@ -106,10 +106,7 @@ sub add { $item = Koha::Items->find($item_id); unless ($item) { - return $c->render( - status => 404, - openapi => { error => "item_id not found." } - ); + return $c->resource_not_found("item_id"); } else { $biblio = $item->biblio; @@ -246,12 +243,8 @@ sub edit { return try { my $hold = Koha::Holds->find( $c->param('hold_id') ); - unless ($hold) { - return $c->render( - status => 404, - openapi => { error => "Hold not found" } - ); - } + return $c->resource_not_found("Hold") + unless $hold; my $overrides = $c->stash('koha.overrides'); my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride'); @@ -310,9 +303,8 @@ sub delete { my $hold = Koha::Holds->find($c->param('hold_id')); - unless ($hold) { - return $c->render( status => 404, openapi => { error => "Hold not found." } ); - } + return $c->resource_not_found("Hold") + unless $hold; return try { @@ -392,9 +384,8 @@ sub resume { my $hold = Koha::Holds->find($c->param('hold_id')); my $body = $c->req->json; - unless ($hold) { - return $c->render( status => 404, openapi => { error => 'Hold not found.' } ); - } + return $c->resource_not_found("Hold") + unless $hold; return try { $hold->resume; @@ -416,12 +407,8 @@ sub update_priority { my $hold = Koha::Holds->find($c->param('hold_id')); - unless ($hold) { - return $c->render( - status => 404, - openapi => { error => "Hold not found" } - ); - } + return $c->resource_not_found("Hold") + unless $hold; return try { my $priority = $c->req->json; @@ -451,12 +438,8 @@ sub pickup_locations { my $hold = Koha::Holds->find( $c->param('hold_id'), { prefetch => [ 'patron' ] } ); - unless ($hold) { - return $c->render( - status => 404, - openapi => { error => "Hold not found" } - ); - } + return $c->resource_not_found("Hold") + unless $hold; return try { my $ps_set; @@ -516,13 +499,8 @@ sub update_pickup_location { my $hold = Koha::Holds->find($c->param('hold_id')); - unless ($hold) { - return $c->render( - status => 404, - openapi => { error => "Hold not found" } - ); - } - + return $c->resource_not_found("Hold") + unless $hold; return try { diff --git a/Koha/REST/V1/IllbatchStatuses.pm b/Koha/REST/V1/IllbatchStatuses.pm index 1527dd3875d..7229163bcfe 100644 --- a/Koha/REST/V1/IllbatchStatuses.pm +++ b/Koha/REST/V1/IllbatchStatuses.pm @@ -54,12 +54,8 @@ sub get { my $status = Koha::IllbatchStatuses->find( { code => $status_code } ); - if ( not defined $status ) { - return $c->render( - status => 404, - openapi => { error => "ILL batch status not found" } - ); - } + return $c->resource_not_found("ILL batch status") + unless $status; return $c->render( status => 200, @@ -109,12 +105,8 @@ sub update { my $status = Koha::IllbatchStatuses->find( { code => $c->param('ill_batchstatus_code') } ); - if ( not defined $status ) { - return $c->render( - status => 404, - openapi => { error => "ILL batch status not found" } - ); - } + return $c->resource_not_found("ILL batch status") + unless $status; my $params = $c->req->json; @@ -144,9 +136,8 @@ sub delete { my $status = Koha::IllbatchStatuses->find( { code => $c->param('ill_batchstatus_code') } ); - if ( not defined $status ) { - return $c->render( status => 404, openapi => { errors => [ { message => "ILL batch status not found" } ] } ); - } + return $c->resource_not_found("ILL batch status") + unless $status; if ( $status->is_system ) { return $c->render( diff --git a/Koha/REST/V1/Illbatches.pm b/Koha/REST/V1/Illbatches.pm index b4e7d6aa026..5131443a7d5 100644 --- a/Koha/REST/V1/Illbatches.pm +++ b/Koha/REST/V1/Illbatches.pm @@ -63,12 +63,8 @@ sub get { return try { my $ill_batch = $c->objects->find( Koha::Illbatches->new, $c->param('ill_batch_id') ); - unless ($ill_batch) { - return $c->render( - status => 404, - openapi => { error => "ILL batch not found" } - ); - } + return $c->resource_not_found("ILL batch") + unless $ill_batch; return $c->render( status => 200, @@ -92,12 +88,8 @@ sub add { my $patron = Koha::Patrons->find( { cardnumber => $body->{cardnumber} } ); - if ( not defined $patron ) { - return $c->render( - status => 404, - openapi => { error => "Patron with cardnumber " . $body->{cardnumber} . " not found" } - ); - } + return $c->resource_not_found( "Patron with cardnumber " . $body->{cardnumber} ) + unless $patron; delete $body->{cardnumber}; $body->{patron_id} = $patron->id; @@ -138,12 +130,8 @@ sub update { my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') ); - unless ($batch) { - return $c->render( - status => 404, - openapi => { error => "ILL batch not found" } - ); - } + return $c->resource_not_found("ILL batch") + unless $batch; my $params = $c->req->json; delete $params->{cardnumber}; @@ -172,9 +160,8 @@ sub delete { my $batch = Koha::Illbatches->find( $c->param('ill_batch_id') ); - if ( not defined $batch ) { - return $c->render( status => 404, openapi => { error => "ILL batch not found" } ); - } + return $c->resource_not_found("ILL batch") + unless $batch; return try { $batch->delete_and_log; diff --git a/Koha/REST/V1/ImportBatchProfiles.pm b/Koha/REST/V1/ImportBatchProfiles.pm index 562bae39a2d..16b5bd634ab 100644 --- a/Koha/REST/V1/ImportBatchProfiles.pm +++ b/Koha/REST/V1/ImportBatchProfiles.pm @@ -89,12 +89,9 @@ sub edit { return try { my $profile = Koha::ImportBatchProfiles->find( $c->param('import_batch_profile_id') ); - unless ($profile) { - return $c->render( - status => 404, - openapi => {error => "Import batch profile not found"} - ); - } + + return $c->resource_not_found("Import batch profile") + unless $profile; $profile->set_from_api($c->req->json)->store; @@ -119,10 +116,8 @@ sub delete { my $profile = Koha::ImportBatchProfiles->find( $c->param('import_batch_profile_id') ); - unless ($profile) { - return $c->render( status => 404, - openapi => {error => "Import batch profile not found"} ); - } + return $c->resource_not_found("Import batch profile") + unless $profile; return try { $profile->delete; diff --git a/Koha/REST/V1/ImportRecordMatches.pm b/Koha/REST/V1/ImportRecordMatches.pm index 64ffd1f8dd8..007b8134a48 100644 --- a/Koha/REST/V1/ImportRecordMatches.pm +++ b/Koha/REST/V1/ImportRecordMatches.pm @@ -43,12 +43,10 @@ sub unset_chosen { my $matches = Koha::Import::Record::Matches->search({ import_record_id => $c->param('import_record_id'), }); - unless ($matches) { - return $c->render( - status => 404, - openapi => { error => "No matches not found" } - ); - } + + return $c->resource_not_found("Matches") + unless $matches; + return try { $matches->update({ chosen => 0 }); return $c->render( status => 204, openapi => $matches ); @@ -80,12 +78,8 @@ sub set_chosen { candidate_match_id => $candidate_match_id }); - unless ($match) { - return $c->render( - status => 404, - openapi => { error => "Match not found" } - ); - } + return $c->resource_not_found("Match") + unless $match; return try { my $matches = Koha::Import::Record::Matches->search({ diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm index 69333c1bdf6..2b58e42ff05 100644 --- a/Koha/REST/V1/Items.pm +++ b/Koha/REST/V1/Items.pm @@ -96,12 +96,10 @@ sub get { try { my $items_rs = Koha::Items->new; my $item = $c->objects->find($items_rs, $c->param('item_id')); - unless ( $item ) { - return $c->render( - status => 404, - openapi => { error => 'Item not found'} - ); - } + + return $c->resource_not_found("Item") + unless $item; + return $c->render( status => 200, openapi => $item ); } catch { @@ -120,12 +118,9 @@ sub delete { return try { my $item = Koha::Items->find($c->param('item_id')); - unless ( $item ) { - return $c->render( - status => 404, - openapi => { error => 'Item not found'} - ); - } + + return $c->resource_not_found("Item") + unless $item; my $safe_to_delete = $item->safe_to_delete; @@ -185,12 +180,8 @@ sub get_bookings { my $item = Koha::Items->find( { itemnumber => $c->param('item_id') }, { prefetch => ['bookings'] } ); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Object not found." } - ); - } + return $c->resource_not_found("Object") + unless $item; return try { @@ -218,12 +209,8 @@ sub pickup_locations { my $item_id = $c->param('item_id'); my $item = Koha::Items->find( $item_id ); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; my $patron_id = $c->param('patron_id'); my $patron = Koha::Patrons->find( $patron_id ); @@ -286,12 +273,8 @@ sub bundled_items { my $item_id = $c->param('item_id'); my $item = Koha::Items->find( $item_id ); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; return try { my $items_set = Koha::Items->search( @@ -325,12 +308,8 @@ sub add_to_bundle { my $item_id = $c->param('item_id'); my $item = Koha::Items->find( $item_id ); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; my $body = $c->req->json; @@ -338,12 +317,8 @@ sub add_to_bundle { $bundle_item_id = barcodedecode($bundle_item_id); my $bundle_item = Koha::Items->find( { barcode => $bundle_item_id } ); - unless ($bundle_item) { - return $c->render( - status => 404, - openapi => { error => "Bundle item not found" } - ); - } + return $c->resource_not_found("Bundle item") + unless $bundle_item; return try { my $options = { @@ -430,12 +405,8 @@ sub remove_from_bundle { my $item_id = $c->param('item_id'); my $item = Koha::Items->find( $item_id ); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; my $bundle_item_id = $c->param('bundled_item_id'); $bundle_item_id = barcodedecode($bundle_item_id); diff --git a/Koha/REST/V1/Libraries.pm b/Koha/REST/V1/Libraries.pm index db0cb3011de..092d41afe94 100644 --- a/Koha/REST/V1/Libraries.pm +++ b/Koha/REST/V1/Libraries.pm @@ -64,12 +64,8 @@ sub get { return try { my $library = Koha::Libraries->find( $c->param('library_id') ); - unless ($library) { - return $c->render( - status => 404, - openapi => { error => "Library not found" } - ); - } + return $c->resource_not_found("Library") + unless $library; return $c->render( status => 200, @@ -122,12 +118,8 @@ sub update { my $library = Koha::Libraries->find( $c->param('library_id') ); - if ( not defined $library ) { - return $c->render( - status => 404, - openapi => { error => "Library not found" } - ); - } + return $c->resource_not_found("Library") + unless $library; return try { my $params = $c->req->json; @@ -155,9 +147,8 @@ sub delete { my $library = Koha::Libraries->find( $c->param( 'library_id' ) ); - if ( not defined $library ) { - return $c->render( status => 404, openapi => { error => "Library not found" } ); - } + return $c->resource_not_found("Library") + unless $library; return try { $library->delete; @@ -183,12 +174,8 @@ sub list_desks { return try { my $library = Koha::Libraries->find( $c->param('library_id') ); - unless ($library) { - return $c->render( - status => 404, - openapi => { error => "Library not found" } - ); - } + return $c->resource_not_found("Library") + unless $library; return $c->render( status => 200, @@ -214,12 +201,8 @@ sub list_cash_registers { return try { my $library = Koha::Libraries->find( $c->param('library_id') ); - unless ($library) { - return $c->render( - status => 404, - openapi => { error => "Library not found" } - ); - } + return $c->resource_not_found("Library") + unless $library; return $c->render( status => 200, diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 0c37a4aeb68..fe40b58371d 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -79,12 +79,8 @@ sub get { my $patron_id = $c->param('patron_id'); my $patron = $c->objects->find( Koha::Patrons->search_limited, $patron_id ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return $c->render( status => 200, @@ -264,12 +260,8 @@ sub update { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found" } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $body = $c->req->json; @@ -382,12 +374,8 @@ sub delete { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ( $patron ) { - return $c->render( - status => 404, - openapi => { error => "Patron not found" } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm index 7e941c33260..d3e38300724 100644 --- a/Koha/REST/V1/Patrons/Account.pm +++ b/Koha/REST/V1/Patrons/Account.pm @@ -42,12 +42,8 @@ sub get { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $account = $patron->account; @@ -85,12 +81,8 @@ sub list_credits { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $credits = $c->objects->search( $patron->account->credits ); @@ -113,12 +105,8 @@ sub add_credit { my $patron = Koha::Patrons->find( $c->param('patron_id') ); my $user = $c->stash('koha.user'); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; my $account = $patron->account; my $body = $c->req->json; @@ -199,12 +187,8 @@ sub list_debits { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $debits = $c->objects->search( $patron->account->debits ); @@ -224,12 +208,8 @@ sub add_debit { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { error => "Patron not found." } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $data = diff --git a/Koha/REST/V1/Patrons/Attributes.pm b/Koha/REST/V1/Patrons/Attributes.pm index 269cab4856e..cc341ee0b68 100644 --- a/Koha/REST/V1/Patrons/Attributes.pm +++ b/Koha/REST/V1/Patrons/Attributes.pm @@ -45,14 +45,8 @@ sub list_patron_attributes { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { @@ -79,14 +73,8 @@ sub add { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { @@ -151,14 +139,8 @@ sub overwrite { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { @@ -229,27 +211,15 @@ sub update { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $attribute = $patron->extended_attributes->find( $c->param('extended_attribute_id') ); - unless ($attribute) { - return $c->render( - status => 404, - openapi => { - error => 'Attribute not found' - } - ); - } + return $c->resource_not_found("Attribute") + unless $attribute; $attribute->set_from_api( $c->req->json )->store; $attribute->discard_changes; @@ -303,28 +273,16 @@ sub delete { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { my $attribute = $patron->extended_attributes->find( $c->param('extended_attribute_id') ); - unless ($attribute) { - return $c->render( - status => 404, - openapi => { - error => 'Attribute not found' - } - ); - } + return $c->resource_not_found("Attribute") + unless $attribute; $attribute->delete; return $c->render( diff --git a/Koha/REST/V1/Patrons/Holds.pm b/Koha/REST/V1/Patrons/Holds.pm index 88eea15b537..d6259802bd9 100644 --- a/Koha/REST/V1/Patrons/Holds.pm +++ b/Koha/REST/V1/Patrons/Holds.pm @@ -40,14 +40,8 @@ sub list { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ( $patron ) { - return $c->render( - status => 404, - openapi => { - error => 'Patron not found' - } - ); - } + return $c->resource_not_found("Patron") + unless $patron; return try { @@ -79,10 +73,7 @@ sub delete_public { my $hold = $c->objects->find_rs( Koha::Holds->new, $c->param('hold_id') ); unless ( $hold and $c->param('patron_id') == $hold->borrowernumber ) { - return $c->render( - status => 404, - openapi => { error => 'Object not found' } - ); + return $c->resource_not_found("Object"); } if ( $hold->is_cancelable_from_opac ) { diff --git a/Koha/REST/V1/Patrons/Password.pm b/Koha/REST/V1/Patrons/Password.pm index 4b4b89dea9a..d8fc2486a47 100644 --- a/Koha/REST/V1/Patrons/Password.pm +++ b/Koha/REST/V1/Patrons/Password.pm @@ -47,9 +47,8 @@ sub set { my $patron = Koha::Patrons->find( $c->param('patron_id') ); my $body = $c->req->json; - unless ($patron) { - return $c->render( status => 404, openapi => { error => "Patron not found." } ); - } + return $c->resource_not_found("Patron") + unless $patron; my $password = $body->{password} // ""; my $password_2 = $body->{password_2} // ""; diff --git a/Koha/REST/V1/Patrons/Password/Expiration.pm b/Koha/REST/V1/Patrons/Password/Expiration.pm index a038071ab0f..851ac63d795 100644 --- a/Koha/REST/V1/Patrons/Password/Expiration.pm +++ b/Koha/REST/V1/Patrons/Password/Expiration.pm @@ -45,9 +45,8 @@ sub set { my $patron = Koha::Patrons->find( $c->param('patron_id') ); - unless ($patron) { - return $c->render( status => 404, openapi => { error => "Patron not found." } ); - } + return $c->resource_not_found("Patron") + unless $patron; my $body = $c->req->json; diff --git a/Koha/REST/V1/Preservation/Processings.pm b/Koha/REST/V1/Preservation/Processings.pm index 70e1f9d5817..d4c9a21b127 100644 --- a/Koha/REST/V1/Preservation/Processings.pm +++ b/Koha/REST/V1/Preservation/Processings.pm @@ -57,12 +57,8 @@ sub get { my $processing_id = $c->param('processing_id'); my $processing = $c->objects->find( Koha::Preservation::Processings->search, $processing_id ); - unless ($processing) { - return $c->render( - status => 404, - openapi => { error => "Processing not found" } - ); - } + return $c->resource_not_found("Processing") + unless $processing; return $c->render( status => 200, @@ -144,12 +140,8 @@ sub update { my $processing_id = $c->param('processing_id'); my $processing = Koha::Preservation::Processings->find($processing_id); - unless ($processing) { - return $c->render( - status => 404, - openapi => { error => "Processing not found" } - ); - } + return $c->resource_not_found("Processing") + unless $processing; return try { Koha::Database->new->schema->txn_do( @@ -204,12 +196,9 @@ sub delete { my $processing_id = $c->param('processing_id'); my $processing = Koha::Preservation::Processings->find($processing_id); - unless ($processing) { - return $c->render( - status => 404, - openapi => { error => "Processing not found" } - ); - } + + return $c->resource_not_found("Processing") + unless $processing; unless ( $processing->can_be_deleted ) { return $c->render( diff --git a/Koha/REST/V1/Preservation/Trains.pm b/Koha/REST/V1/Preservation/Trains.pm index a93f7601ae4..d280945ad05 100644 --- a/Koha/REST/V1/Preservation/Trains.pm +++ b/Koha/REST/V1/Preservation/Trains.pm @@ -60,12 +60,8 @@ sub get { my $train_id = $c->param('train_id'); my $train = $c->objects->find( Koha::Preservation::Trains->search, $train_id ); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; return $c->render( status => 200, @@ -144,12 +140,8 @@ sub update { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; return try { Koha::Database->new->schema->txn_do( @@ -202,12 +194,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $train = Koha::Preservation::Trains->find( $c->param('train_id') ); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + + return $c->resource_not_found("Train") + unless $train; return try { $train->delete; @@ -232,12 +221,8 @@ sub get_item { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $train_item_id = $c->param('train_item_id'); @@ -276,12 +261,8 @@ sub add_items { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $body = $c->req->json; return try { @@ -317,12 +298,8 @@ sub add_item { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $body = $c->req->json; return try { @@ -385,24 +362,16 @@ sub copy_item { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $train_item_id = $c->param('train_item_id'); my $train_item = Koha::Preservation::Train::Items->search( { train_item_id => $train_item_id, train_id => $train_id } )->single; - unless ($train_item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $train_item; my $body = $c->req->json; return try { @@ -489,24 +458,16 @@ sub update_item { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $train_item_id = $c->param('train_item_id'); my $train_item = Koha::Preservation::Train::Items->search( { train_item_id => $train_item_id, train_id => $train_id } )->single; - unless ($train_item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $train_item; return try { Koha::Database->new->schema->txn_do( @@ -536,23 +497,15 @@ sub remove_item { my $train_id = $c->param('train_id'); my $train = Koha::Preservation::Trains->find($train_id); - unless ($train) { - return $c->render( - status => 404, - openapi => { error => "Train not found" } - ); - } + return $c->resource_not_found("Train") + unless $train; my $train_item_id = $c->param('train_item_id'); my $train_item = $train->items->find($train_item_id); - unless ($train_item) { - return $c->render( - status => 404, - openapi => { error => "Train item not found" } - ); - } + return $c->resource_not_found("Train item") + unless $train_item; return try { $train_item->delete; diff --git a/Koha/REST/V1/Preservation/WaitingList.pm b/Koha/REST/V1/Preservation/WaitingList.pm index a909f02f1cf..4fb937a180d 100644 --- a/Koha/REST/V1/Preservation/WaitingList.pm +++ b/Koha/REST/V1/Preservation/WaitingList.pm @@ -130,12 +130,8 @@ sub remove_item { my $item = Koha::Items->find($item_id); - unless ($item) { - return $c->render( - status => 404, - openapi => { error => "Item not found" } - ); - } + return $c->resource_not_found("Item") + unless $item; my $not_for_loan_waiting_list_in = C4::Context->preference('PreservationNotForLoanWaitingListIn'); if ( $item->notforloan ne $not_for_loan_waiting_list_in ) { diff --git a/Koha/REST/V1/Quotes.pm b/Koha/REST/V1/Quotes.pm index 88845122039..f2048716a59 100644 --- a/Koha/REST/V1/Quotes.pm +++ b/Koha/REST/V1/Quotes.pm @@ -53,12 +53,9 @@ sub get { return try { my $quote = Koha::Quotes->find( $c->param('quote_id') ); - unless ($quote) { - return $c->render( - status => 404, - openapi => { error => "quote not found" } - ); - } + + return $c->resource_not_found("quote") + unless $quote; return $c->render( status => 200, openapi => $c->objects->to_api($quote), ); } @@ -97,10 +94,8 @@ sub update { my $quote = Koha::Quotes->find( $c->param('quote_id') ); - if ( not defined $quote ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found("Object") + unless $quote; return try { $quote->set_from_api( $c->req->json ); @@ -120,10 +115,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $quote = Koha::Quotes->find( $c->param('quote_id') ); - if ( not defined $quote ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + + return $c->resource_not_found("Object") + unless $quote; return try { $quote->delete; diff --git a/Koha/REST/V1/ReturnClaims.pm b/Koha/REST/V1/ReturnClaims.pm index e3ce9b30f15..5366e5816fc 100644 --- a/Koha/REST/V1/ReturnClaims.pm +++ b/Koha/REST/V1/ReturnClaims.pm @@ -53,10 +53,8 @@ sub claim_returned { my $checkout = Koha::Checkouts->find( { itemnumber => $itemnumber } ); - return $c->render( - openapi => { error => "Checkout not found" }, - status => 404 - ) unless $checkout; + return $c->resource_not_found("Checkout") + unless $checkout; my $claim = $checkout->claim_returned( { @@ -104,12 +102,8 @@ sub update_notes { my $claim = Koha::Checkouts::ReturnClaims->find( $claim_id ); - return $c->render( - status => 404, - openapi => { - error => "Claim not found" - } - ) unless $claim; + return $c->resource_not_found("Claim") + unless $claim; return try { my $updated_by = $body->{updated_by}; @@ -150,10 +144,8 @@ sub resolve_claim { my $claim = Koha::Checkouts::ReturnClaims->find($claim_id); - return $c->render( - status => 404, - openapi => { error => "Claim not found" } - ) unless $claim; + return $c->resource_not_found("Claim") + unless $claim; return try { @@ -195,10 +187,8 @@ sub delete_claim { my $claim = Koha::Checkouts::ReturnClaims->find( $c->param('claim_id') ); - return $c->render( - status => 404, - openapi => { error => "Claim not found" } - ) unless $claim; + return $c->resource_not_found("Claim") + unless $claim; $claim->delete(); diff --git a/Koha/REST/V1/SearchFilter.pm b/Koha/REST/V1/SearchFilter.pm index 895d3cf472a..9f6e21f3aca 100644 --- a/Koha/REST/V1/SearchFilter.pm +++ b/Koha/REST/V1/SearchFilter.pm @@ -59,10 +59,9 @@ Controller function that handles retrieving a single Koha::AdvancedEditorMacro sub get { my $c = shift->openapi->valid_input or return; my $filter = Koha::SearchFilters->find( $c->param('search_filter_id') ); - unless ($filter) { - return $c->render( status => 404, - openapi => { error => "Search filter not found" } ); - } + + return $c->resource_not_found("Search filter") + unless $filter; return $c->render( status => 200, openapi => $c->objects->to_api($filter), ); } @@ -107,10 +106,8 @@ sub update { my $filter = Koha::SearchFilters->find( $c->param('search_filter_id') ); - if ( not defined $filter ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + return $c->resource_not_found("Object") + unless $filter; return try { $filter->set_from_api( $c->req->json ); @@ -132,10 +129,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $filter = Koha::SearchFilters->find( $c->param('search_filter_id') ); - if ( not defined $filter ) { - return $c->render( status => 404, - openapi => { error => "Object not found" } ); - } + + return $c->resource_not_found("Object") + unless $filter; return try { $filter->delete; diff --git a/Koha/REST/V1/Suggestions.pm b/Koha/REST/V1/Suggestions.pm index b6432e2a5ff..ddb6eb23507 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -65,12 +65,8 @@ sub get { return try { my $suggestion = $c->objects->find( Koha::Suggestions->new, $c->param('suggestion_id') ); - unless ($suggestion) { - return $c->render( - status => 404, - openapi => { error => "Suggestion not found." } - ); - } + return $c->resource_not_found("Suggestion") + unless $suggestion; return $c->render( status => 200, @@ -168,10 +164,8 @@ sub update { my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') ); - return $c->render( - status => 404, - openapi => { error => 'Suggestion not found.' } - ) unless $suggestion; + return $c->resource_not_found("Suggestion") + unless $suggestion; return try { @@ -202,10 +196,8 @@ sub delete { my $suggestion = Koha::Suggestions->find( $c->param('suggestion_id') ); - return $c->render( - status => 404, - openapi => { error => 'Suggestion not found.' } - ) unless $suggestion; + return $c->resource_not_found("Suggestion") + unless $suggestion; return try { $suggestion->delete; diff --git a/Koha/REST/V1/Tickets.pm b/Koha/REST/V1/Tickets.pm index b42cee64129..07e7fe7c0e6 100644 --- a/Koha/REST/V1/Tickets.pm +++ b/Koha/REST/V1/Tickets.pm @@ -56,12 +56,8 @@ sub get { return try { my $ticket = Koha::Tickets->find( $c->param('ticket_id') ); - unless ($ticket) { - return $c->render( - status => 404, - openapi => { error => "Ticket not found" } - ); - } + return $c->resource_not_found("Ticket") + unless $ticket; return $c->render( status => 200, openapi => $c->objects->to_api($ticket), ); } @@ -109,12 +105,8 @@ sub update { my $ticket = Koha::Tickets->find( $c->param('ticket_id') ); - if ( not defined $ticket ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + return $c->resource_not_found("Object") + unless $ticket; return try { $ticket->set_from_api( $c->req->json ); @@ -134,12 +126,9 @@ sub delete { my $c = shift->openapi->valid_input or return; my $ticket = Koha::Tickets->find( $c->param('ticket_id') ); - if ( not defined $ticket ) { - return $c->render( - status => 404, - openapi => { error => "Object not found" } - ); - } + + return $c->resource_not_found("Object") + unless $ticket; return try { $ticket->delete; @@ -162,12 +151,9 @@ sub list_updates { return try { my $ticket = Koha::Tickets->find( $c->param('ticket_id') ); - unless ($ticket) { - return $c->render( - status => 404, - openapi => { error => "Ticket not found" } - ); - } + + return $c->resource_not_found("Ticket") + unless $ticket; my $updates_set = $ticket->updates; my $updates = $c->objects->search($updates_set); diff --git a/t/db_dependent/api/v1/article_requests.t b/t/db_dependent/api/v1/article_requests.t index 0bf1b8d73b4..6e43ffd6257 100755 --- a/t/db_dependent/api/v1/article_requests.t +++ b/t/db_dependent/api/v1/article_requests.t @@ -55,7 +55,7 @@ subtest 'cancel() tests' => sub { $t->delete_ok( "//$userid:$password@/api/v1/article_requests/$deleted_article_request_id" - )->status_is(404)->json_is( { error => "Article request not found" } ); + )->status_is(404)->json_is( '/error_code' => 'not_found' ); my $article_request = $builder->build_object( { @@ -109,7 +109,7 @@ subtest 'patron_cancel() tests' => sub { # delete non existent article request $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/$deleted_article_request_id") ->status_is(404) - ->json_is( { error => "Article request not found" } ); + ->json_is( '/error_code' => 'not_found' ); my $another_patron = $builder->build_object({ class => 'Koha::Patrons' }); my $another_patron_id = $another_patron->id; @@ -130,7 +130,7 @@ subtest 'patron_cancel() tests' => sub { $t->delete_ok("//$userid:$password@/api/v1/public/patrons/$patron_id/article_requests/" . $another_article_request->id) ->status_is(404) - ->json_is( { error => 'Article request not found' } ); + ->json_is( '/error_code' => 'not_found' ); my $article_request = $builder->build_object( { diff --git a/t/db_dependent/api/v1/authorities.t b/t/db_dependent/api/v1/authorities.t index 85da554eb1b..68e24e48e21 100755 --- a/t/db_dependent/api/v1/authorities.t +++ b/t/db_dependent/api/v1/authorities.t @@ -108,7 +108,7 @@ subtest 'get() tests' => sub { $t->get_ok( "//$userid:$password@/api/v1/authorities/" . $authority->id => { Accept => 'application/marc' } ) ->status_is(404) - ->json_is( '/error', 'Object not found.' ); + ->json_is( '/error', 'Object not found' ); $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index fdb148a8807..d6d2b4f5d9d 100755 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -105,7 +105,7 @@ subtest 'get() tests' => sub { $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber => { Accept => 'application/marc' } ) ->status_is(404) - ->json_is( '/error', 'Object not found.' ); + ->json_is( '/error', 'Object not found' ); subtest 'marc-in-json encoding tests' => sub { @@ -408,7 +408,7 @@ subtest 'get_public() tests' => sub { $t->get_ok( "//$userid:$password@/api/v1/public/biblios/" . $biblio->biblionumber => { Accept => 'application/marc' } ) ->status_is(404) - ->json_is( '/error', 'Object not found.' ); + ->json_is( '/error', 'Object not found' ); $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/erm_eholdings_titles.t b/t/db_dependent/api/v1/erm_eholdings_titles.t index e2f12a62ade..2c7802f1c1b 100755 --- a/t/db_dependent/api/v1/erm_eholdings_titles.t +++ b/t/db_dependent/api/v1/erm_eholdings_titles.t @@ -309,11 +309,8 @@ subtest 'add() tests' => sub { # Attempt to import titles from a virtualshelf that doesn't exist $virtual_shelf->delete; - $t->post_ok( - "//$userid:$password@/api/v1/erm/eholdings/local/titles/import" => json => - $import_request )->status_is(404)->json_is( - { error => 'List not found' } - ); + $t->post_ok( "//$userid:$password@/api/v1/erm/eholdings/local/titles/import" => json => $import_request ) + ->status_is(404)->json_is( '/error' => 'List not found' ); # Authorized attempt to create with null id $ehtitle->{title_id} = undef; diff --git a/t/db_dependent/api/v1/patrons.t b/t/db_dependent/api/v1/patrons.t index a3247d7de93..60a63e9511c 100755 --- a/t/db_dependent/api/v1/patrons.t +++ b/t/db_dependent/api/v1/patrons.t @@ -363,9 +363,8 @@ subtest 'get() tests' => sub { ->status_is(200) ->json_is( '/patron_id' => $patron_1->id ); - $t->get_ok("//$userid:$password@/api/v1/patrons/" . $patron_2->id ) - ->status_is(404) - ->json_is({ error => "Patron not found." }); + $t->get_ok( "//$userid:$password@/api/v1/patrons/" . $patron_2->id )->status_is(404) + ->json_is( '/error' => 'Patron not found' ); $schema->storage->txn_rollback; }; diff --git a/t/db_dependent/api/v1/suggestions.t b/t/db_dependent/api/v1/suggestions.t index b7b06935277..494650174cb 100755 --- a/t/db_dependent/api/v1/suggestions.t +++ b/t/db_dependent/api/v1/suggestions.t @@ -153,7 +153,7 @@ subtest 'get() tests' => sub { $t->get_ok( "//$userid:$password@/api/v1/suggestions/$non_existent_id") - ->status_is(404)->json_is( '/error' => 'Suggestion not found.' ); + ->status_is(404)->json_is( '/error' => 'Suggestion not found' ); $schema->storage->txn_rollback; }; -- 2.44.0