From 63524f0ebdd00141a932475f82f7a5cadc4e336b Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Tue, 4 Sep 2012 16:23:03 +0100 Subject: API errors should be JSON The API was returning Rails (HTML) errors for certain error conditions, which is inconvenient because it makes it difficult for the client to extract the error message. This patch changes add_correspondence to return JSON errors (still with suitable HTTP status codes) for two common exceptional conditions, and adds tests. Conflicts: spec/controllers/api_controller_spec.rb --- app/controllers/api_controller.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'app/controllers/api_controller.rb') diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 524aa44b7..26950aaf3 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -72,7 +72,12 @@ class ApiController < ApplicationController end def add_correspondence - request = InfoRequest.find(params[:id]) + request = InfoRequest.find_by_id(params[:id]) + if request.nil? + render :json => { "errors" => ["Could not find request #{params[:id]}"] }, :status => 404 + return + end + json = ActiveSupport::JSON.decode(params[:correspondence_json]) attachments = params[:attachments] @@ -83,11 +88,13 @@ class ApiController < ApplicationController errors = [] if !request.is_external? - raise ActiveRecord::RecordNotFound.new("Request #{params[:id]} cannot be updated using the API") + render :json => { "errors" => ["Request #{params[:id]} cannot be updated using the API"] }, :status => 500 + return end if request.public_body_id != @public_body.id - raise ActiveRecord::RecordNotFound.new("You do not own request #{params[:id]}") + render :json => { "errors" => ["You do not own request #{params[:id]}"] }, :status => 500 + return end if !["request", "response"].include?(direction) -- cgit v1.2.3