From eb1464f558fe87d2acfd445f6e96ae3de550327b Mon Sep 17 00:00:00 2001 From: Andreas Granig Date: Wed, 12 Feb 2014 14:18:08 +0100 Subject: [PATCH] MT#3925 Return json error for 403/404 on /api/.+ Return as usual a json struct with code and message under the /api/ path. Otherwise an API client would get the html pages, which is pretty pointless. --- lib/NGCP/Panel/Controller/Root.pm | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index 20eca51447..7b33c10a88 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -214,9 +214,14 @@ sub _prune_row { sub error_page :Private { my ($self,$c) = @_; - $c->log->error( 'Failed to find path ' . $c->request->path ); - $c->stash(template => 'notfound_page.tt'); + + if($c->request->path =~ /^api\/.+/) { + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ code => 404, message => 'Path not found' })."\n"); + } else { + $c->stash(template => 'notfound_page.tt'); + } $c->response->status(404); } @@ -224,7 +229,12 @@ sub denied_page :Private { my ($self,$c) = @_; $c->log->error('Access denied to path ' . $c->request->path ); - $c->stash(template => 'denied_page.tt'); + if($c->request->path =~ /^api\/.+/) { + $c->response->content_type('application/json'); + $c->response->body(JSON::to_json({ code => 403, message => 'Path forbidden' })."\n"); + } else { + $c->stash(template => 'denied_page.tt'); + } $c->response->status(403); }