From 5b9d36cb39d03dc9b3279cc886e1a9e20efbce10 Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Fri, 1 May 2015 20:51:40 +0000 Subject: [PATCH] Ensure AR not found errors are handled in API --- app/controllers/api/application_controller.rb | 6 ++++++ app/controllers/api/errors_controller.rb | 7 ------- config/routes.rb | 2 +- spec/integration/api/application_spec.rb | 8 +++++++- 4 files changed, 14 insertions(+), 9 deletions(-) delete mode 100644 app/controllers/api/errors_controller.rb diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb index b8de05d..bb644fb 100644 --- a/app/controllers/api/application_controller.rb +++ b/app/controllers/api/application_controller.rb @@ -1,11 +1,17 @@ module API class ApplicationController < ::ApplicationController + rescue_from ActiveRecord::RecordNotFound, with: :not_found + skip_before_filter :verify_authenticity_token skip_before_filter :authenticate!, only: :cor_preflight before_filter :cor_filter before_filter :json_filter! + def not_found + head :not_found + end + def cor_filter headers['Access-Control-Allow-Origin'] = request.headers['Origin'] ? request.headers['Origin'] : diff --git a/app/controllers/api/errors_controller.rb b/app/controllers/api/errors_controller.rb deleted file mode 100644 index d0c9d78..0000000 --- a/app/controllers/api/errors_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -module API - class ErrorsController < ApplicationController - def not_found - head :not_found - end - end -end diff --git a/config/routes.rb b/config/routes.rb index 0c6d8b2..70f0e84 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,7 +8,7 @@ Rails.application.routes.draw do resources :sessions, only: :create resources :sounds, only: :show resources :tracks, only: :index - match '*all', to: 'errors#not_found', via: :all + match '*all', to: 'application#not_found', via: :all end resources :playlists diff --git a/spec/integration/api/application_spec.rb b/spec/integration/api/application_spec.rb index df92c4f..a86ff4f 100644 --- a/spec/integration/api/application_spec.rb +++ b/spec/integration/api/application_spec.rb @@ -22,7 +22,13 @@ describe 'API application' do describe 'not found' do it 'responds with a 404 when route does not exist' do get '/api/not_found', format: :json - expect(response).to be_not_found + expect(response.status).to be 404 + expect(response.body).to be_empty + end + + it 'responds with a 404 when a resource (AR) was not found' do + get api_playlist_path(id: 1), format: :json + expect(response.status).to be 404 expect(response.body).to be_empty end end