Skip to content

Commit

Permalink
add not found handle
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 8, 2018
1 parent 0f37829 commit bb019b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
class ApplicationController < ActionController::API
rescue_from ActionController::RoutingError do |e|
render json: {
message: e.message
}, status: :not_found
end

def not_found
raise ActionController::RoutingError, "not found"
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
resources :status, only: [:index]
resources :sync_errors, only: [:index]
end

# 404
match '*path', via: :all, to: 'application#not_found'
end
14 changes: 14 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe ApplicationController, type: :request do
let(:body) { Oj.load(response.body) }

context "not found" do
it "demo" do
post '/hhh'

expect(response).to have_http_status(404)
expect(body["message"]).to eq "not found"
end
end
end

0 comments on commit bb019b7

Please sign in to comment.