From 52e2751b50bd9eddec6d80fcaf5b1bc4e831a729 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Wed, 20 Mar 2019 21:18:12 +0000 Subject: [PATCH] fix errors for api to be json --- lib/App/Netdisco/Util/Web.pm | 2 +- lib/App/Netdisco/Web.pm | 5 +++++ lib/App/Netdisco/Web/Plugin.pm | 5 +++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/App/Netdisco/Util/Web.pm b/lib/App/Netdisco/Util/Web.pm index ff4451ab..35d9c1f8 100644 --- a/lib/App/Netdisco/Util/Web.pm +++ b/lib/App/Netdisco/Util/Web.pm @@ -37,7 +37,7 @@ Whether the request should be interpreted as an API call. sub request_is_api { return (setting('api_token_lifetime') 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? ) diff --git a/lib/App/Netdisco/Web.pm b/lib/App/Netdisco/Web.pm index e65279d2..f0fd5f9f 100644 --- a/lib/App/Netdisco/Web.pm +++ b/lib/App/Netdisco/Web.pm @@ -123,6 +123,11 @@ hook after_error_render => sub { setting('layout' => 'main') }; 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 { my $key = request->path; if (param('tab') and ($key !~ m/ajax/)) { diff --git a/lib/App/Netdisco/Web/Plugin.pm b/lib/App/Netdisco/Web/Plugin.pm index bff80d9f..889df8e1 100644 --- a/lib/App/Netdisco/Web/Plugin.pm +++ b/lib/App/Netdisco/Web/Plugin.pm @@ -4,7 +4,6 @@ use Dancer ':syntax'; use Dancer::Plugin; use App::Netdisco::Util::Web 'request_is_api'; -use Dancer::Plugin::REST 'status_bad_request'; use Path::Class 'dir'; set( @@ -28,7 +27,9 @@ config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('vi register 'bang' => sub { if (request_is_api()) { content_type('application/json'); - status_bad_request(@_); + set serializer => 'JSON'; + status $_[1]; + { error => $_[0] }; } else { send_error(@_) } };