From fd19cf79c1132c17b6a9460b259e328ca9308714 Mon Sep 17 00:00:00 2001
From: Andreas Granig <agranig@sipwise.com>
Date: Mon, 2 Dec 2013 12:24:36 +0100
Subject: [PATCH] MT#3997 Return JSON on precondition failures.

---
 lib/NGCP/Panel/Controller/API/ContactsItem.pm  | 10 ++--------
 lib/NGCP/Panel/Controller/API/ContractsItem.pm | 10 ++--------
 2 files changed, 4 insertions(+), 16 deletions(-)

diff --git a/lib/NGCP/Panel/Controller/API/ContactsItem.pm b/lib/NGCP/Panel/Controller/API/ContactsItem.pm
index eb98c038da..f6ad8bed71 100644
--- a/lib/NGCP/Panel/Controller/API/ContactsItem.pm
+++ b/lib/NGCP/Panel/Controller/API/ContactsItem.pm
@@ -119,10 +119,7 @@ sub PATCH :Allow {
                 last unless $self->resource_exists($c, contact => $contact);
                 $entity = JSON::decode_json($self->hal_from_contact($contact)->as_json);
             } else {
-                $c->response->status(HTTP_PRECONDITION_FAILED);
-                $c->response->header('Content-Language' => 'en');
-                $c->response->content_type('application/xhtml+xml');
-                $c->stash(template => 'api/precondition_failed.tt', entity_name => 'contact');
+	    	$self->error($c, HTTP_PRECONDITION_FAILED, "This 'contact' entity cannot be found, it is either expired or does not exist. Fetch a fresh one.");
                 last;
             }
         }
@@ -246,10 +243,7 @@ sub PUT :Allow {
                 last unless $self->resource_exists($c, contact => $contact);
                 $entity = JSON::decode_json($self->hal_from_contact($contact)->as_json);
             } else {
-                $c->response->status(HTTP_PRECONDITION_FAILED);
-                $c->response->header('Content-Language' => 'en');
-                $c->response->content_type('application/xhtml+xml');
-                $c->stash(template => 'api/precondition_failed.tt', entity_name => 'contact');
+	    	$self->error($c, HTTP_PRECONDITION_FAILED, "This 'contact' entity cannot be found, it is either expired or does not exist. Fetch a fresh one.");
                 last;
             }
         }
diff --git a/lib/NGCP/Panel/Controller/API/ContractsItem.pm b/lib/NGCP/Panel/Controller/API/ContractsItem.pm
index 0ffd82995f..a4309859e8 100644
--- a/lib/NGCP/Panel/Controller/API/ContractsItem.pm
+++ b/lib/NGCP/Panel/Controller/API/ContractsItem.pm
@@ -121,10 +121,7 @@ sub PATCH :Allow {
                 last unless $self->resource_exists($c, contract => $contract);
                 $entity = JSON::decode_json($self->hal_from_contract($contract)->as_json);
             } else {
-                $c->response->status(HTTP_PRECONDITION_FAILED);
-                $c->response->header('Content-Language' => 'en');
-                $c->response->content_type('application/xhtml+xml');
-                $c->stash(template => 'api/precondition_failed.tt', entity_name => 'contract');
+	    	$self->error($c, HTTP_PRECONDITION_FAILED, "This 'contract' entity cannot be found, it is either expired or does not exist. Fetch a fresh one.");
                 last;
             }
         }
@@ -233,10 +230,7 @@ sub PUT :Allow {
                 last unless $self->resource_exists($c, contract => $contract);
                 $entity = JSON::decode_json($self->hal_from_contract($contract)->as_json);
             } else {
-                $c->response->status(HTTP_PRECONDITION_FAILED);
-                $c->response->header('Content-Language' => 'en');
-                $c->response->content_type('application/xhtml+xml');
-                $c->stash(template => 'api/precondition_failed.tt', entity_name => 'contract');
+	    	$self->error($c, HTTP_PRECONDITION_FAILED, "This 'contract' entity cannot be found, it is either expired or does not exist. Fetch a fresh one.");
                 last;
             }
         }