fix errors for api to be json

This commit is contained in:
Oliver Gorwits
2019-03-20 21:18:12 +00:00
parent 1e32f802f8
commit 52e2751b50
3 changed files with 9 additions and 3 deletions

View File

@@ -37,7 +37,7 @@ Whether the request should be interpreted as an API call.
sub request_is_api { sub request_is_api {
return (setting('api_token_lifetime') return (setting('api_token_lifetime')
and request->accept =~ m/(?:json|javascript)/ and request->accept =~ m/(?:json|javascript)/
and index(request->path, uri_for('/api')->path) == 0); and index(var('orig_path'), uri_for('/api')->path) == 0);
} }
=head2 sql_match( $value, $exact? ) =head2 sql_match( $value, $exact? )

View File

@@ -123,6 +123,11 @@ hook after_error_render => sub { setting('layout' => 'main') };
for @port_columns; for @port_columns;
} }
# support for checking if this is an api request even after forward
hook 'before' => sub {
vars->{'orig_path'} = request->path unless request->is_forward;
};
hook 'before' => sub { hook 'before' => sub {
my $key = request->path; my $key = request->path;
if (param('tab') and ($key !~ m/ajax/)) { if (param('tab') and ($key !~ m/ajax/)) {

View File

@@ -4,7 +4,6 @@ use Dancer ':syntax';
use Dancer::Plugin; use Dancer::Plugin;
use App::Netdisco::Util::Web 'request_is_api'; use App::Netdisco::Util::Web 'request_is_api';
use Dancer::Plugin::REST 'status_bad_request';
use Path::Class 'dir'; use Path::Class 'dir';
set( set(
@@ -28,7 +27,9 @@ config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('vi
register 'bang' => sub { register 'bang' => sub {
if (request_is_api()) { if (request_is_api()) {
content_type('application/json'); content_type('application/json');
status_bad_request(@_); set serializer => 'JSON';
status $_[1];
{ error => $_[0] };
} }
else { send_error(@_) } else { send_error(@_) }
}; };