View | Details | Raw Unified | Return to bug 25032
Collapse All | Expand All

(-)a/Koha/Exceptions/ClubHold.pm (-1 / +1 lines)
Lines 12-15 use Exception::Class ( Link Here
12
    },
12
    },
13
);
13
);
14
14
15
1;
15
1;
(-)a/Koha/REST/V1/Acquisitions/Funds.pm (-8 / +1 lines)
Lines 51-64 sub list { Link Here
51
        );
51
        );
52
    }
52
    }
53
    catch {
53
    catch {
54
        if ( $_->isa('DBIx::Class::Exception') ) {
54
        $c->unhandled_exception($_);
55
            return $c->render( status  => 500,
56
                               openapi => { error => $_->{msg} } );
57
        }
58
        else {
59
            return $c->render( status  => 500,
60
                               openapi => { error => "Something went wrong, check the logs. $_" } );
61
        }
62
    };
55
    };
63
}
56
}
64
57
(-)a/Koha/REST/V1/Acquisitions/Orders.pm (-57 / +16 lines)
Lines 52-69 sub list { Link Here
52
        );
52
        );
53
    }
53
    }
54
    catch {
54
    catch {
55
        if ( $_->isa('Koha::Exceptions::Exception') ) {
55
        $c->unhandled_exception($_);
56
            return $c->render(
57
                status  => 500,
58
                openapi => { error => "Unhandled exception ($_)" }
59
            );
60
        }
61
        else {
62
            return $c->render(
63
                status  => 500,
64
                openapi => { error => "Something went wrong, check the logs. ($_)" }
65
            );
66
        }
67
    };
56
    };
68
}
57
}
69
58
Lines 85-96 sub get { Link Here
85
        );
74
        );
86
    }
75
    }
87
76
88
    my $embed = $c->stash('koha.embed');
77
    return try {
78
        my $embed = $c->stash('koha.embed');
89
79
90
    return $c->render(
80
        return $c->render(
91
        status  => 200,
81
            status  => 200,
92
        openapi => $order->to_api({ embed => $embed })
82
            openapi => $order->to_api({ embed => $embed })
93
    );
83
        );
84
    }
85
    catch {
86
        $c->unhandled_exception($_);
87
    };
94
}
88
}
95
89
96
=head3 add
90
=head3 add
Lines 116-142 sub add { Link Here
116
        );
110
        );
117
    }
111
    }
118
    catch {
112
    catch {
119
        unless ( blessed $_ && $_->can('rethrow') ) {
113
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
120
            return $c->render(
121
                status  => 500,
122
                openapi => {
123
                    error =>
124
                      "Something went wrong, check Koha logs for details."
125
                }
126
            );
127
        }
128
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
129
            return $c->render(
114
            return $c->render(
130
                status  => 409,
115
                status  => 409,
131
                openapi => { error => $_->error, conflict => $_->duplicate_id }
116
                openapi => { error => $_->error, conflict => $_->duplicate_id }
132
            );
117
            );
133
        }
118
        }
134
        else {
119
135
            return $c->render(
120
        $c->unhandled_exception($_);
136
                status  => 500,
137
                openapi => { error => "$_" }
138
            );
139
        }
140
    };
121
    };
141
}
122
}
142
123
Lines 168-185 sub update { Link Here
168
        );
149
        );
169
    }
150
    }
170
    catch {
151
    catch {
171
        if ( $_->isa('Koha::Exceptions::Exception') ) {
152
        $c->unhandled_exception($_);
172
            return $c->render(
173
                status  => 500,
174
                openapi => { error => "Unhandled exception ($_)" }
175
            );
176
        }
177
        else {
178
            return $c->render(
179
                status  => 500,
180
                openapi => { error => "Something went wrong, check the logs." }
181
            );
182
        }
183
    };
153
    };
184
}
154
}
185
155
Lines 211-228 sub delete { Link Here
211
        );
181
        );
212
    }
182
    }
213
    catch {
183
    catch {
214
        if ( $_->isa('Koha::Exceptions::Exception') ) {
184
        $c->unhandled_exception($_);
215
            return $c->render(
216
                status  => 500,
217
                openapi => { error => "Unhandled exception ($_)" }
218
            );
219
        }
220
        else {
221
            return $c->render(
222
                status  => 500,
223
                openapi => { error => "Something went wrong, check the logs." }
224
            );
225
        }
226
    };
185
    };
227
}
186
}
228
187
(-)a/Koha/REST/V1/Acquisitions/Vendors.pm (-48 / +33 lines)
Lines 49-62 sub list { Link Here
49
        );
49
        );
50
    }
50
    }
51
    catch {
51
    catch {
52
        if ( $_->isa('DBIx::Class::Exception') ) {
52
        $c->unhandled_exception($_);
53
            return $c->render( status  => 500,
54
                               openapi => { error => $_->{msg} } );
55
        }
56
        else {
57
            return $c->render( status  => 500,
58
                               openapi => { error => "Something went wrong, check the logs." } );
59
        }
60
    };
53
    };
61
}
54
}
62
55
Lines 75-84 sub get { Link Here
75
                           openapi => { error => "Vendor not found" } );
68
                           openapi => { error => "Vendor not found" } );
76
    }
69
    }
77
70
78
    return $c->render(
71
    return try {
79
        status  => 200,
72
        return $c->render(
80
        openapi => $vendor->to_api
73
            status  => 200,
81
    );
74
            openapi => $vendor->to_api
75
        );
76
    }
77
    catch {
78
        $c->unhandled_exception($_);
79
    };
82
}
80
}
83
81
84
=head3 add
82
=head3 add
Lines 101-114 sub add { Link Here
101
        );
99
        );
102
    }
100
    }
103
    catch {
101
    catch {
104
        if ( $_->isa('DBIx::Class::Exception') ) {
102
        $c->unhandled_exception($_);
105
            return $c->render( status  => 500,
106
                               openapi => { error => $_->msg } );
107
        }
108
        else {
109
            return $c->render( status  => 500,
110
                               openapi => { error => "Something went wrong, check the logs." } );
111
        }
112
    };
103
    };
113
}
104
}
114
105
Lines 134-150 sub update { Link Here
134
    }
125
    }
135
    catch {
126
    catch {
136
        if ( not defined $vendor ) {
127
        if ( not defined $vendor ) {
137
            return $c->render( status  => 404,
128
            return $c->render(
138
                               openapi => { error => "Object not found" } );
129
                status  => 404,
139
        }
130
                openapi => { error => "Object not found" }
140
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
131
            );
141
            return $c->render( status  => 500,
142
                               openapi => { error => $_->message } );
143
        }
144
        else {
145
            return $c->render( status  => 500,
146
                               openapi => { error => "Something went wrong, check the logs." } );
147
        }
132
        }
133
134
        $c->unhandled_exception($_);
135
148
    };
136
    };
149
137
150
}
138
}
Lines 158-186 Controller function that handles deleting a Koha::Acquisition::Bookseller object Link Here
158
sub delete {
146
sub delete {
159
    my $c = shift->openapi->valid_input or return;
147
    my $c = shift->openapi->valid_input or return;
160
148
161
    my $vendor;
162
163
    return try {
149
    return try {
164
        $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
150
        my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
151
152
        unless ( $vendor ) {
153
            return $c->render(
154
                status  => 404,
155
                openapi => { error => "Object not found" }
156
            );
157
        }
158
165
        $vendor->delete;
159
        $vendor->delete;
166
        return $c->render( status => 200,
160
167
                           openapi => q{} );
161
        return $c->render(
162
            status  => 200,
163
            openapi => q{}
164
        );
168
    }
165
    }
169
    catch {
166
    catch {
170
        if ( not defined $vendor ) {
167
        $c->unhandled_exception($_);
171
            return $c->render( status  => 404,
172
                               openapi => { error => "Object not found" } );
173
        }
174
        elsif ( $_->isa('DBIx::Class::Exception') ) {
175
            return $c->render( status  => 500,
176
                               openapi => { error => $_->msg } );
177
        }
178
        else {
179
            return $c->render( status  => 500,
180
                               openapi => { error => "Something went wrong, check the logs." } );
181
        }
182
    };
168
    };
183
184
}
169
}
185
170
186
1;
171
1;
(-)a/Koha/REST/V1/Biblios.pm (-16 / +2 lines)
Lines 94-103 sub get { Link Here
94
        }
94
        }
95
    }
95
    }
96
    catch {
96
    catch {
97
        return $c->render(
97
        $c->unhandled_exception($_);
98
            status  => 500,
99
            openapi => { error => "Something went wrong, check the logs ($_)" }
100
        );
101
    };
98
    };
102
}
99
}
103
100
Lines 133-150 sub delete { Link Here
133
        }
130
        }
134
    }
131
    }
135
    catch {
132
    catch {
136
        if ( $_->isa('DBIx::Class::Exception') ) {
133
        $c->unhandled_exception($_);
137
            return $c->render(
138
                status  => 500,
139
                openapi => { error => $_->{msg} }
140
            );
141
        }
142
        else {
143
            return $c->render(
144
                status  => 500,
145
                openapi => { error => "Something went wrong, check the logs." }
146
            );
147
        }
148
    };
134
    };
149
}
135
}
150
136
(-)a/Koha/REST/V1/Checkouts.pm (-52 / +57 lines)
Lines 100-116 sub list { Link Here
100
100
101
        return $c->render( status => 200, openapi => $checkouts->to_api );
101
        return $c->render( status => 200, openapi => $checkouts->to_api );
102
    } catch {
102
    } catch {
103
        if ( $_->isa('DBIx::Class::Exception') ) {
103
        $c->unhandled_exception($_);
104
            return $c->render(
105
                status => 500,
106
                openapi => { error => $_->{msg} }
107
            );
108
        } else {
109
            return $c->render(
110
                status => 500,
111
                openapi => { error => "Something went wrong, check the logs." }
112
            );
113
        }
114
    };
104
    };
115
}
105
}
116
106
Lines 135-144 sub get { Link Here
135
        );
125
        );
136
    }
126
    }
137
127
138
    return $c->render(
128
    return try {
139
        status  => 200,
129
        return $c->render(
140
        openapi => $checkout->to_api
130
            status  => 200,
141
    );
131
            openapi => $checkout->to_api
132
        );
133
    }
134
    catch {
135
        $c->unhandled_exception($_);
136
    };
142
}
137
}
143
138
144
=head3 renew
139
=head3 renew
Lines 160-186 sub renew { Link Here
160
        );
155
        );
161
    }
156
    }
162
157
163
    my $borrowernumber = $checkout->borrowernumber;
158
    return try {
164
    my $itemnumber = $checkout->itemnumber;
159
        my $borrowernumber = $checkout->borrowernumber;
160
        my $itemnumber = $checkout->itemnumber;
161
162
        my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
163
            $borrowernumber, $itemnumber);
164
165
        if (!$can_renew) {
166
            return $c->render(
167
                status => 403,
168
                openapi => { error => "Renewal not authorized ($error)" }
169
            );
170
        }
165
171
166
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
172
        AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
167
        $borrowernumber, $itemnumber);
173
        $checkout = Koha::Checkouts->find($checkout_id);
168
174
169
    if (!$can_renew) {
175
        $c->res->headers->location( $c->req->url->to_string );
170
        return $c->render(
176
        return $c->render(
171
            status => 403,
177
            status  => 201,
172
            openapi => { error => "Renewal not authorized ($error)" }
178
            openapi => $checkout->to_api
173
        );
179
        );
174
    }
180
    }
175
181
    catch {
176
    AddRenewal($borrowernumber, $itemnumber, $checkout->branchcode);
182
        $c->unhandled_exception($_);
177
    $checkout = Koha::Checkouts->find($checkout_id);
183
    };
178
179
    $c->res->headers->location( $c->req->url->to_string );
180
    return $c->render(
181
        status  => 201,
182
        openapi => $checkout->to_api
183
    );
184
}
184
}
185
185
186
=head3 allows_renewal
186
=head3 allows_renewal
Lines 202-230 sub allows_renewal { Link Here
202
        );
202
        );
203
    }
203
    }
204
204
205
    my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
205
    return try {
206
        $checkout->borrowernumber, $checkout->itemnumber);
206
        my ($can_renew, $error) = C4::Circulation::CanBookBeRenewed(
207
            $checkout->borrowernumber, $checkout->itemnumber);
207
208
208
    my $renewable = Mojo::JSON->false;
209
        my $renewable = Mojo::JSON->false;
209
    $renewable = Mojo::JSON->true if $can_renew;
210
        $renewable = Mojo::JSON->true if $can_renew;
210
211
211
    my $rule = Koha::CirculationRules->get_effective_rule(
212
        my $rule = Koha::CirculationRules->get_effective_rule(
212
        {
213
            {
213
            categorycode => $checkout->patron->categorycode,
214
                categorycode => $checkout->patron->categorycode,
214
            itemtype     => $checkout->item->effective_itemtype,
215
                itemtype     => $checkout->item->effective_itemtype,
215
            branchcode   => $checkout->branchcode,
216
                branchcode   => $checkout->branchcode,
216
            rule_name    => 'renewalsallowed',
217
                rule_name    => 'renewalsallowed',
217
        }
218
            }
218
    );
219
        );
219
    return $c->render(
220
        return $c->render(
220
        status => 200,
221
            status => 200,
221
        openapi => {
222
            openapi => {
222
            allows_renewal => $renewable,
223
                allows_renewal => $renewable,
223
            max_renewals => $rule->rule_value,
224
                max_renewals => $rule->rule_value,
224
            current_renewals => $checkout->renewals,
225
                current_renewals => $checkout->renewals,
225
            error => $error
226
                error => $error
226
        }
227
            }
227
    );
228
        );
229
    }
230
    catch {
231
        $c->unhandled_exception($_);
232
    };
228
}
233
}
229
234
230
1;
235
1;
(-)a/Koha/REST/V1/Cities.pm (-42 / +15 lines)
Lines 40-53 sub list { Link Here
40
        return $c->render( status => 200, openapi => $cities );
40
        return $c->render( status => 200, openapi => $cities );
41
    }
41
    }
42
    catch {
42
    catch {
43
        if ( $_->isa('DBIx::Class::Exception') ) {
43
        $c->unhandled_exception($_);
44
            return $c->render( status  => 500,
45
                               openapi => { error => $_->{msg} } );
46
        }
47
        else {
48
            return $c->render( status => 500,
49
                openapi => { error => "Something went wrong, check the logs."} );
50
        }
51
    };
44
    };
52
45
53
}
46
}
Lines 59-71 sub list { Link Here
59
sub get {
52
sub get {
60
    my $c = shift->openapi->valid_input or return;
53
    my $c = shift->openapi->valid_input or return;
61
54
62
    my $city = Koha::Cities->find( $c->validation->param('city_id') );
55
    return try {
63
    unless ($city) {
56
        my $city = Koha::Cities->find( $c->validation->param('city_id') );
64
        return $c->render( status  => 404,
57
        unless ($city) {
65
                           openapi => { error => "City not found" } );
58
            return $c->render( status  => 404,
66
    }
59
                            openapi => { error => "City not found" } );
60
        }
67
61
68
    return $c->render( status => 200, openapi => $city->to_api );
62
        return $c->render( status => 200, openapi => $city->to_api );
63
    }
64
    catch {
65
        $c->unhandled_exception($_);
66
    }
69
}
67
}
70
68
71
=head3 add
69
=head3 add
Lines 85-102 sub add { Link Here
85
        );
83
        );
86
    }
84
    }
87
    catch {
85
    catch {
88
        if ( $_->isa('DBIx::Class::Exception') ) {
86
        $c->unhandled_exception($_);
89
            return $c->render(
90
                status  => 500,
91
                openapi => { error => $_->{msg} }
92
            );
93
        }
94
        else {
95
            return $c->render(
96
                status  => 500,
97
                openapi => { error => "Something went wrong, check the logs." }
98
            );
99
        }
100
    };
87
    };
101
}
88
}
102
89
Lines 120-133 sub update { Link Here
120
        return $c->render( status => 200, openapi => $city->to_api );
107
        return $c->render( status => 200, openapi => $city->to_api );
121
    }
108
    }
122
    catch {
109
    catch {
123
        if ( $_->isa('Koha::Exceptions::Object') ) {
110
        $c->unhandled_exception($_);
124
            return $c->render( status  => 500,
125
                               openapi => { error => $_->message } );
126
        }
127
        else {
128
            return $c->render( status => 500,
129
                openapi => { error => "Something went wrong, check the logs."} );
130
        }
131
    };
111
    };
132
}
112
}
133
113
Lines 149-162 sub delete { Link Here
149
        return $c->render( status => 200, openapi => "" );
129
        return $c->render( status => 200, openapi => "" );
150
    }
130
    }
151
    catch {
131
    catch {
152
        if ( $_->isa('DBIx::Class::Exception') ) {
132
        $c->unhandled_exception($_);
153
            return $c->render( status  => 500,
154
                               openapi => { error => $_->{msg} } );
155
        }
156
        else {
157
            return $c->render( status => 500,
158
                openapi => { error => "Something went wrong, check the logs."} );
159
        }
160
    };
133
    };
161
}
134
}
162
135
(-)a/Koha/REST/V1/Clubs/Holds.pm (-21 / +5 lines)
Lines 125-131 sub add { Link Here
125
        );
125
        );
126
    }
126
    }
127
    catch {
127
    catch {
128
        if ( blessed $_ and $_->isa('Koha::Exceptions::Object') ) {
128
        if ( blessed $_ ) {
129
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
129
            if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
130
                my $broken_fk = $_->broken_fk;
130
                my $broken_fk = $_->broken_fk;
131
131
Lines 135-166 sub add { Link Here
135
                        openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.'
135
                        openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.'
136
                    );
136
                    );
137
                }
137
                }
138
                else {
139
                    return $c->render(
140
                        status  => 500,
141
                        openapi => { error => "Uncaught exception: $_" }
142
                    );
143
                }
144
            }
138
            }
145
            else {
139
            elsif ($_->isa('Koha::Exceptions::ClubHold')) {
146
                return $c->render(
140
                return $c->render(
147
                    status  => 500,
141
                    status  => 500,
148
                    openapi => { error => "$_" }
142
                    openapi => { error => $_->description }
149
                );
143
                );
150
            }
144
            }
151
        }
145
        }
152
        elsif (blessed $_ and $_->isa('Koha::Exceptions::ClubHold')) {
146
153
            return $c->render(
147
        $c->unhandled_exception($_);
154
                status  => 500,
155
                openapi => { error => $_->description }
156
            );
157
        }
158
        else {
159
            return $c->render(
160
                status  => 500,
161
                openapi => { error => "Something went wrong. check the logs." }
162
            );
163
        }
164
    };
148
    };
165
}
149
}
166
150
(-)a/Koha/REST/V1/Holds.pm (-70 / +43 lines)
Lines 48-65 sub list { Link Here
48
        return $c->render( status => 200, openapi => $holds );
48
        return $c->render( status => 200, openapi => $holds );
49
    }
49
    }
50
    catch {
50
    catch {
51
        if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
51
        $c->unhandled_exception($_);
52
            return $c->render(
53
                status  => 500,
54
                openapi => { error => "$_" }
55
            );
56
        }
57
        else {
58
            return $c->render(
59
                status  => 500,
60
                openapi => { error => "Something went wrong, check Koha logs for details." }
61
            );
62
        }
63
    };
52
    };
64
}
53
}
65
54
Lines 201-226 sub add { Link Here
201
                        openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
190
                        openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
202
                    );
191
                    );
203
                }
192
                }
204
                else {
205
                    return $c->render(
206
                        status  => 500,
207
                        openapi => { error => "Uncaught exception: $_" }
208
                    );
209
                }
210
            }
211
            else {
212
                return $c->render(
213
                    status  => 500,
214
                    openapi => { error => "$_" }
215
                );
216
            }
193
            }
217
        }
194
        }
218
        else {
195
219
            return $c->render(
196
        $c->unhandled_exception($_);
220
                status  => 500,
221
                openapi => { error => "Something went wrong. check the logs." }
222
            );
223
        }
224
    };
197
    };
225
}
198
}
226
199
Lines 233-268 Method that handles modifying a Koha::Hold object Link Here
233
sub edit {
206
sub edit {
234
    my $c = shift->openapi->valid_input or return;
207
    my $c = shift->openapi->valid_input or return;
235
208
236
    my $hold_id = $c->validation->param('hold_id');
209
    return try {
237
    my $hold = Koha::Holds->find( $hold_id );
210
        my $hold_id = $c->validation->param('hold_id');
211
        my $hold = Koha::Holds->find( $hold_id );
238
212
239
    unless ($hold) {
213
        unless ($hold) {
240
        return $c->render( status  => 404,
214
            return $c->render( status  => 404,
241
                           openapi => {error => "Hold not found"} );
215
                            openapi => {error => "Hold not found"} );
242
    }
216
        }
243
217
244
    my $body = $c->req->json;
218
        my $body = $c->req->json;
245
219
246
    my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
220
        my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
247
    my $priority          = $body->{priority} // $hold->priority;
221
        my $priority          = $body->{priority} // $hold->priority;
248
    # suspended_until can also be set to undef
222
        # suspended_until can also be set to undef
249
    my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
223
        my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
250
224
251
    my $params = {
225
        my $params = {
252
        reserve_id    => $hold_id,
226
            reserve_id    => $hold_id,
253
        branchcode    => $pickup_library_id,
227
            branchcode    => $pickup_library_id,
254
        rank          => $priority,
228
            rank          => $priority,
255
        suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
229
            suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
256
        itemnumber    => $hold->itemnumber
230
            itemnumber    => $hold->itemnumber
257
    };
231
        };
258
232
259
    C4::Reserves::ModReserve($params);
233
        C4::Reserves::ModReserve($params);
260
    $hold->discard_changes; # refresh
234
        $hold->discard_changes; # refresh
261
235
262
    return $c->render(
236
        return $c->render(
263
        status  => 200,
237
            status  => 200,
264
        openapi => $hold->to_api
238
            openapi => $hold->to_api
265
    );
239
        );
240
    }
241
    catch {
242
        $c->unhandled_exception($_);
243
    };
266
}
244
}
267
245
268
=head3 delete
246
=head3 delete
Lines 281-289 sub delete { Link Here
281
        return $c->render( status => 404, openapi => { error => "Hold not found." } );
259
        return $c->render( status => 404, openapi => { error => "Hold not found." } );
282
    }
260
    }
283
261
284
    $hold->cancel;
262
    return try {
263
        $hold->cancel;
285
264
286
    return $c->render( status => 200, openapi => {} );
265
        return $c->render( status => 200, openapi => {} );
266
    }
267
    catch {
268
        $c->unhandled_exception($_);
269
    };
287
}
270
}
288
271
289
=head3 suspend
272
=head3 suspend
Lines 329-340 sub suspend { Link Here
329
        if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
312
        if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
330
            return $c->render( status => 400, openapi => { error => "$_" } );
313
            return $c->render( status => 400, openapi => { error => "$_" } );
331
        }
314
        }
332
        else {
315
333
            return $c->render(
316
        $c->unhandled_exception($_);
334
                status  => 500,
335
                openapi => { error => "Something went wrong. check the logs." }
336
            );
337
        }
338
    };
317
    };
339
}
318
}
340
319
Lines 360-369 sub resume { Link Here
360
        return $c->render( status => 204, openapi => {} );
339
        return $c->render( status => 204, openapi => {} );
361
    }
340
    }
362
    catch {
341
    catch {
363
        return $c->render(
342
        $c->unhandled_exception($_);
364
            status  => 500,
365
            openapi => { error => "Something went wrong. check the logs." }
366
        );
367
    };
343
    };
368
}
344
}
369
345
Lines 398-407 sub update_priority { Link Here
398
        return $c->render( status => 200, openapi => $priority );
374
        return $c->render( status => 200, openapi => $priority );
399
    }
375
    }
400
    catch {
376
    catch {
401
        return $c->render(
377
        $c->unhandled_exception($_);
402
            status  => 500,
403
            openapi => { error => "Something went wrong. check the logs." }
404
        );
405
    };
378
    };
406
}
379
}
407
380
(-)a/Koha/REST/V1/Items.pm (-27 / +9 lines)
Lines 51-69 sub list { Link Here
51
        );
51
        );
52
    }
52
    }
53
    catch {
53
    catch {
54
        unless ( blessed $_ && $_->can('rethrow') ) {
54
        $c->unhandled_exception($_);
55
            return $c->render(
56
                status  => 500,
57
                openapi => {
58
                    error =>
59
                      "Something went wrong, check Koha logs for details."
60
                }
61
            );
62
        }
63
        return $c->render(
64
            status  => 500,
65
            openapi => { error => "$_" }
66
        );
67
    };
55
    };
68
}
56
}
69
57
Lines 77-100 Controller function that handles retrieving a single Koha::Item Link Here
77
sub get {
65
sub get {
78
    my $c = shift->openapi->valid_input or return;
66
    my $c = shift->openapi->valid_input or return;
79
67
80
    my $item;
81
    try {
68
    try {
82
        $item = Koha::Items->find($c->validation->param('item_id'));
69
        my $item = Koha::Items->find($c->validation->param('item_id'));
70
        unless ( $item ) {
71
            return $c->render(
72
                status => 404,
73
                openapi => { error => 'Item not found'}
74
            );
75
        }
83
        return $c->render( status => 200, openapi => $item->to_api );
76
        return $c->render( status => 200, openapi => $item->to_api );
84
    }
77
    }
85
    catch {
78
    catch {
86
        unless ( defined $item ) {
79
        $c->unhandled_exception($_);
87
            return $c->render( status => 404,
88
                               openapi => { error => 'Item not found'} );
89
        }
90
        if ( $_->isa('DBIx::Class::Exception') ) {
91
            return $c->render( status  => 500,
92
                               openapi => { error => $_->{msg} } );
93
        }
94
        else {
95
            return $c->render( status => 500,
96
                openapi => { error => "Something went wrong, check the logs."} );
97
        }
98
    };
80
    };
99
}
81
}
100
82
(-)a/Koha/REST/V1/Libraries.pm (-55 / +21 lines)
Lines 49-64 sub list { Link Here
49
        return $c->render( status => 200, openapi => $libraries );
49
        return $c->render( status => 200, openapi => $libraries );
50
    }
50
    }
51
    catch {
51
    catch {
52
        unless ( blessed $_ && $_->can('rethrow') ) {
52
        $c->unhandled_exception($_);
53
            return $c->render(
54
                status  => 500,
55
                openapi => { error => "Something went wrong, check Koha logs for details." }
56
            );
57
        }
58
        return $c->render(
59
            status  => 500,
60
            openapi => { error => "$_" }
61
        );
62
    };
53
    };
63
}
54
}
64
55
Lines 71-88 Controller function that handles retrieving a single Koha::Library Link Here
71
sub get {
62
sub get {
72
    my $c = shift->openapi->valid_input or return;
63
    my $c = shift->openapi->valid_input or return;
73
64
74
    my $library_id = $c->validation->param('library_id');
65
    return try {
75
    my $library = Koha::Libraries->find( $library_id );
66
        my $library_id = $c->validation->param('library_id');
67
        my $library = Koha::Libraries->find( $library_id );
76
68
77
    unless ($library) {
69
        unless ($library) {
78
        return $c->render( status  => 404,
70
            return $c->render( status  => 404,
79
                           openapi => { error => "Library not found" } );
71
                            openapi => { error => "Library not found" } );
80
    }
72
        }
81
73
82
    return $c->render(
74
        return $c->render(
83
        status  => 200,
75
            status  => 200,
84
        openapi => $library->to_api
76
            openapi => $library->to_api
85
    );
77
        );
78
    }
79
    catch {
80
        $c->unhandled_exception($_);
81
    };
86
}
82
}
87
83
88
=head3 add
84
=head3 add
Lines 105-128 sub add { Link Here
105
        );
101
        );
106
    }
102
    }
107
    catch {
103
    catch {
108
        unless ( blessed $_ && $_->can('rethrow') ) {
104
        if ( blessed $_ && $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
109
            return $c->render(
110
                status  => 500,
111
                openapi => { error => "Something went wrong, check Koha logs for details." }
112
            );
113
        }
114
        if ( $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
115
            return $c->render(
105
            return $c->render(
116
                status  => 409,
106
                status  => 409,
117
                openapi => { error => $_->error, conflict => $_->duplicate_id }
107
                openapi => { error => $_->error, conflict => $_->duplicate_id }
118
            );
108
            );
119
        }
109
        }
120
        else {
110
121
            return $c->render(
111
        $c->unhandled_exception($_);
122
                status  => 500,
123
                openapi => { error => "$_" }
124
            );
125
        }
126
    };
112
    };
127
}
113
}
128
114
Lines 154-170 sub update { Link Here
154
        );
140
        );
155
    }
141
    }
156
    catch {
142
    catch {
157
        unless ( blessed $_ && $_->can('rethrow') ) {
143
        $c->unhandled_exception($_);
158
            return $c->render(
159
                status  => 500,
160
                openapi => { error => "Something went wrong, check Koha logs for details." }
161
            );
162
        }
163
164
        return $c->render(
165
            status  => 500,
166
            openapi => { error => "$_" }
167
        );
168
    };
144
    };
169
}
145
}
170
146
Lines 189-205 sub delete { Link Here
189
        return $c->render( status => 204, openapi => '');
165
        return $c->render( status => 204, openapi => '');
190
    }
166
    }
191
    catch {
167
    catch {
192
        unless ( blessed $_ && $_->can('rethrow') ) {
168
        $c->unhandled_exception($_);
193
            return $c->render(
194
                status  => 500,
195
                openapi => { error => "Something went wrong, check Koha logs for details." }
196
            );
197
        }
198
199
        return $c->render(
200
            status  => 500,
201
            openapi => { error => "$_" }
202
        );
203
    };
169
    };
204
}
170
}
205
171
(-)a/Koha/REST/V1/Patrons.pm (-50 / +17 lines)
Lines 94-111 sub list { Link Here
94
        return $c->render( status => 200, openapi => $patrons->to_api );
94
        return $c->render( status => 200, openapi => $patrons->to_api );
95
    }
95
    }
96
    catch {
96
    catch {
97
        if ( $_->isa('DBIx::Class::Exception') ) {
97
        $c->unhandled_exception($_);
98
            return $c->render(
99
                status  => 500,
100
                openapi => { error => $_->{msg} }
101
            );
102
        }
103
        else {
104
            return $c->render(
105
                status  => 500,
106
                openapi => { error => "Something went wrong, check the logs." }
107
            );
108
        }
109
    };
98
    };
110
}
99
}
111
100
Lines 119-132 Controller function that handles retrieving a single Koha::Patron object Link Here
119
sub get {
108
sub get {
120
    my $c = shift->openapi->valid_input or return;
109
    my $c = shift->openapi->valid_input or return;
121
110
122
    my $patron_id = $c->validation->param('patron_id');
111
    return try {
123
    my $patron    = Koha::Patrons->find($patron_id);
112
        my $patron_id = $c->validation->param('patron_id');
113
        my $patron    = Koha::Patrons->find($patron_id);
124
114
125
    unless ($patron) {
115
        unless ($patron) {
126
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
116
            return $c->render( status => 404, openapi => { error => "Patron not found." } );
127
    }
117
        }
128
118
129
    return $c->render( status => 200, openapi => $patron->to_api );
119
        return $c->render( status => 200, openapi => $patron->to_api );
120
    }
121
    catch {
122
        $c->unhandled_exception($_);
123
    };
130
}
124
}
131
125
132
=head3 add
126
=head3 add
Lines 185-194 sub add { Link Here
185
            );
179
            );
186
        }
180
        }
187
        else {
181
        else {
188
            return $c->render(
182
            $c->unhandled_exception($_);
189
                status  => 500,
190
                openapi => { error => "Something went wrong, check Koha logs for details." }
191
            );
192
        }
183
        }
193
    };
184
    };
194
}
185
}
Lines 267-279 sub update { Link Here
267
            );
258
            );
268
        }
259
        }
269
        else {
260
        else {
270
            return $c->render(
261
            $c->unhandled_exception($_);
271
                status  => 500,
272
                openapi => {
273
                    error =>
274
                      "Something went wrong, check Koha logs for details."
275
                }
276
            );
277
        }
262
        }
278
    };
263
    };
279
}
264
}
Lines 304-316 sub delete { Link Here
304
            );
289
            );
305
        }
290
        }
306
        else {
291
        else {
307
            return $c->render(
292
            $c->unhandled_exception($_);
308
                status  => 500,
309
                openapi => {
310
                    error =>
311
                      "Something went wrong, check Koha logs for details."
312
                }
313
            );
314
        }
293
        }
315
    };
294
    };
316
}
295
}
Lines 347-359 sub guarantors_can_see_charges { Link Here
347
        }
326
        }
348
    }
327
    }
349
    catch {
328
    catch {
350
        return $c->render(
329
        $c->unhandled_exception($_);
351
            status  => 500,
352
            openapi => {
353
                error =>
354
                  "Something went wrong, check Koha logs for details. $_"
355
            }
356
        );
357
    };
330
    };
358
}
331
}
359
332
Lines 389-401 sub guarantors_can_see_checkouts { Link Here
389
        }
362
        }
390
    }
363
    }
391
    catch {
364
    catch {
392
        return $c->render(
365
        $c->unhandled_exception($_);
393
            status  => 500,
394
            openapi => {
395
                error =>
396
                  "Something went wrong, check Koha logs for details. $_"
397
            }
398
        );
399
    };
366
    };
400
}
367
}
401
368
(-)a/Koha/REST/V1/Patrons/Account.pm (-33 / +26 lines)
Lines 48-73 sub get { Link Here
48
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
48
        return $c->render( status => 404, openapi => { error => "Patron not found." } );
49
    }
49
    }
50
50
51
    my $account = $patron->account;
51
    return try {
52
52
        my $account = $patron->account;
53
    # get outstanding debits and credits
53
54
    my $debits  = $account->outstanding_debits;
54
        # get outstanding debits and credits
55
    my $credits = $account->outstanding_credits;
55
        my $debits  = $account->outstanding_debits;
56
56
        my $credits = $account->outstanding_credits;
57
    return $c->render(
57
58
        status  => 200,
58
        return $c->render(
59
        openapi => {
59
            status  => 200,
60
            balance => $account->balance,
60
            openapi => {
61
            outstanding_debits => {
61
                balance => $account->balance,
62
                total => $debits->total_outstanding,
62
                outstanding_debits => {
63
                lines => $debits->to_api
63
                    total => $debits->total_outstanding,
64
            },
64
                    lines => $debits->to_api
65
            outstanding_credits => {
65
                },
66
                total => $credits->total_outstanding,
66
                outstanding_credits => {
67
                lines => $credits->to_api
67
                    total => $credits->total_outstanding,
68
              }
68
                    lines => $credits->to_api
69
        }
69
                }
70
    );
70
            }
71
        );
72
    }
73
    catch {
74
        $c->unhandled_exception($_);
75
    };
71
}
76
}
72
77
73
=head3 add_credit
78
=head3 add_credit
Lines 141-159 sub add_credit { Link Here
141
        return $c->render( status => 200, openapi => { account_line_id => $credit->id } );
146
        return $c->render( status => 200, openapi => { account_line_id => $credit->id } );
142
    }
147
    }
143
    catch {
148
    catch {
144
        if ( blessed $_ && $_->can('rethrow') ) {
149
        $c->unhandled_exception($_);
145
            return $c->render(
146
                status  => 400,
147
                openapi => { error => "$_" }
148
            );
149
        }
150
        else {
151
            # Exception, rely on the stringified exception
152
            return $c->render(
153
                status  => 500,
154
                openapi => { error => "Something went wrong, check the logs" }
155
            );
156
        }
157
    };
150
    };
158
}
151
}
159
152
(-)a/Koha/REST/V1/Patrons/Password.pm (-9 / +16 lines)
Lines 66-77 sub set { Link Here
66
        return $c->render( status => 200, openapi => "" );
66
        return $c->render( status => 200, openapi => "" );
67
    }
67
    }
68
    catch {
68
    catch {
69
        unless ( blessed $_ && $_->can('rethrow') ) {
69
        if ( blessed $_ and $_->isa('Koha::Exceptions::Password') ) {
70
            return $c->render( status => 500, openapi => { error => "$_" } );
70
            return $c->render(
71
                status  => 400,
72
                openapi => { error => "$_" }
73
            );
71
        }
74
        }
72
75
73
        # an exception was raised. return 400 with the stringified exception
76
        $c->unhandled_exception($_);
74
        return $c->render( status => 400, openapi => { error => "$_" } );
75
    };
77
    };
76
}
78
}
77
79
Lines 117-123 sub set_public { Link Here
117
    return try {
119
    return try {
118
        my $dbh = C4::Context->dbh;
120
        my $dbh = C4::Context->dbh;
119
        unless ( checkpw_internal($dbh, $user->userid, $old_password ) ) {
121
        unless ( checkpw_internal($dbh, $user->userid, $old_password ) ) {
120
            Koha::Exceptions::Authorization::Unauthorized->throw("Invalid password");
122
            return $c->render(
123
                status  => 400,
124
                openapi => { error => "Invalid password" }
125
            );
121
        }
126
        }
122
127
123
        ## Change password
128
        ## Change password
Lines 126-137 sub set_public { Link Here
126
        return $c->render( status => 200, openapi => "" );
131
        return $c->render( status => 200, openapi => "" );
127
    }
132
    }
128
    catch {
133
    catch {
129
        unless ( blessed $_ && $_->can('rethrow') ) {
134
        if ( blessed $_ and $_->isa('Koha::Exceptions::Password') ) {
130
            return $c->render( status => 500, openapi => { error => "$_" } );
135
            return $c->render(
136
                status  => 400,
137
                openapi => { error => "$_" }
138
            );
131
        }
139
        }
132
140
133
        # an exception was raised. return 400 with the stringified exception
141
        $c->unhandled_exception($_);
134
        return $c->render( status => 400, openapi => { error => "$_" } );
135
    };
142
    };
136
}
143
}
137
144
(-)a/Koha/REST/V1/ReturnClaims.pm (-43 / +5 lines)
Lines 86-97 sub claim_returned { Link Here
86
                openapi => { error => "Mandatory attribute created_by missing" }
86
                openapi => { error => "Mandatory attribute created_by missing" }
87
            );
87
            );
88
        }
88
        }
89
        else {
89
90
            return $c->render(
90
        $c->unhandled_exception($_);
91
                status  => 500,
92
                openapi => { error => "Something went wrong, check the logs." }
93
            );
94
        }
95
    };
91
    };
96
}
92
}
97
93
Lines 137-154 sub update_notes { Link Here
137
        );
133
        );
138
    }
134
    }
139
    catch {
135
    catch {
140
        if ( $_->isa('Koha::Exceptions::Exception') ) {
136
        $c->unhandled_exception($_);
141
            return $c->render(
142
                status  => 500,
143
                openapi => { error => "$_" }
144
            );
145
        }
146
        else {
147
            return $c->render(
148
                status  => 500,
149
                openapi => { error => "Something went wrong, check the logs." }
150
            );
151
        }
152
    };
137
    };
153
}
138
}
154
139
Lines 196-213 sub resolve_claim { Link Here
196
        );
181
        );
197
    }
182
    }
198
    catch {
183
    catch {
199
        if ( $_->isa('Koha::Exceptions::Exception') ) {
184
        $c->unhandled_exception($_);
200
            return $c->render(
201
                status  => 500,
202
                openapi => { error => "$_" }
203
            );
204
        }
205
        else {
206
            return $c->render(
207
                status  => 500,
208
                openapi => { error => "Something went wrong, check the logs." }
209
            );
210
        }
211
    };
185
    };
212
}
186
}
213
187
Lines 237-254 sub delete_claim { Link Here
237
        );
211
        );
238
    }
212
    }
239
    catch {
213
    catch {
240
        if ( $_->isa('Koha::Exceptions::Exception') ) {
214
        $c->unhandled_exception($_);
241
            return $c->render(
242
                status  => 500,
243
                openapi => { error => "$_" }
244
            );
245
        }
246
        else {
247
            return $c->render(
248
                status  => 500,
249
                openapi => { error => "Something went wrong, check the logs." }
250
            );
251
        }
252
    };
215
    };
253
}
216
}
254
217
255
- 

Return to bug 25032