From 2c64b67abdb86ae23cfc775a02cd70019bc8ed10 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 8 Oct 2016 12:42:17 +0800 Subject: [PATCH] return 404s for all unmatched api routes --- app/controllers/api/v1/deprecated_controller.rb | 2 +- app/controllers/api/v2/restful_controller.rb | 5 +++++ config/routes.rb | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/deprecated_controller.rb b/app/controllers/api/v1/deprecated_controller.rb index b9e07214..aa67d6b1 100644 --- a/app/controllers/api/v1/deprecated_controller.rb +++ b/app/controllers/api/v1/deprecated_controller.rb @@ -4,7 +4,7 @@ module Api class DeprecatedController < ApplicationController # rubocop:disable Style/MethodMissing def method_missing - render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' } + render json: { error: '/api/v1 is deprecated! Please use /api/v2 instead.' }, status: :gone end # rubocop:enable Style/MethodMissing end diff --git a/app/controllers/api/v2/restful_controller.rb b/app/controllers/api/v2/restful_controller.rb index 5d8f81b3..b64682f3 100644 --- a/app/controllers/api/v2/restful_controller.rb +++ b/app/controllers/api/v2/restful_controller.rb @@ -29,6 +29,11 @@ module Api head :no_content end + def catch_404 + skip_authorization + render json: { error: '404 Not found' }, status: :not_found + end + private def accessible_records diff --git a/config/routes.rb b/config/routes.rb index 05fe5845..62728fe7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -77,6 +77,7 @@ Metamaps::Application.routes.draw do resources :users, only: [:index, :show] do get :current, on: :collection end + match '*path', to: 'restful#catch_404', via: :all end namespace :v1, path: '/v1' do # api v1 routes all lead to a deprecation error method @@ -88,7 +89,9 @@ Metamaps::Application.routes.draw do resources :tokens, only: [:create, :destroy] do get :my_tokens, on: :collection end + match '*path', to: 'deprecated#method_missing', via: :all end + match '*path', to: 'v2/restful#catch_404', via: :all end devise_for :users, skip: :sessions, controllers: {