Skip to content

Commit 9cc7b9d

Browse files
author
Manic Chuang
committed
feat(ErrorHandler): add rendering for other formats
1 parent f913f06 commit 9cc7b9d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

app/controllers/errors_controller.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
class ErrorsController < ApplicationController
22
def not_found
3-
render(status: 404)
3+
respond_to do |format|
4+
format.json { render json: { message: 'Page not found' }, status: 404 }
5+
format.html { render status: 404 }
6+
format.any { render plain: 'Page not found', status: 404 }
7+
end
48
end
59

610
def internal_server_error
7-
render(status: 500)
11+
respond_to do |format|
12+
format.json { render json: { message: 'Internal Server Error' }, status: 500 }
13+
format.html { render status: 500 }
14+
format.any { render plain: 'Internal Server Error', status: 500 }
15+
end
816
end
917
end
+19-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
require 'rails_helper'
22

33
RSpec.describe ErrorsController, type: :request do
4-
it '/404' do
5-
get '/dasdasdasd'
6-
expect(response.status).to eq(404)
7-
expect(response.body).to include('looking for doesn\'t exist.')
4+
context '/404' do
5+
it 'html format' do
6+
get '/dasdasdasd'
7+
expect(response.status).to eq(404)
8+
expect(response.body).to include('looking for doesn\'t exist.')
9+
end
10+
11+
it 'json format' do
12+
get '/not_a_valid_url.json'
13+
expect(response.status).to eq(404)
14+
response_json = JSON.parse(response.body)
15+
expect(response_json['message']).to eq('Page not found')
16+
end
17+
18+
it 'other format' do
19+
get '/not_a_valid_url.test'
20+
expect(response.status).to eq(404)
21+
expect(response.body).to include('Page not found')
22+
end
823
end
924
end

0 commit comments

Comments
 (0)