Skip to content

Commit 15b7b9e

Browse files
committed
Add rescue_from(ActionController::UnknownFormat) in Application Controller
1 parent 57ff963 commit 15b7b9e

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

app/controllers/application_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class ApplicationController < ActionController::Base
4040
render_404
4141
end
4242

43+
rescue_from(ActionController::UnknownFormat) do
44+
render_404
45+
end
46+
4347
rescue_from Gitlab::Access::AccessDeniedError do |exception|
4448
render_403
4549
end

app/controllers/projects/deployments_controller.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ def metrics
2525
def additional_metrics
2626
return render_404 unless deployment.has_additional_metrics?
2727

28-
metrics = deployment.additional_metrics
29-
30-
if metrics.any?
31-
render json: metrics
32-
else
33-
head :no_content
28+
respond_to do |format|
29+
format.json do
30+
metrics = deployment.additional_metrics
31+
32+
if metrics.any?
33+
render json: metrics
34+
else
35+
head :no_content
36+
end
37+
end
3438
end
3539
end
3640

app/controllers/projects/environments_controller.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ def metrics
132132
end
133133

134134
def additional_metrics
135-
additional_metrics = environment.additional_metrics || {}
135+
respond_to do |format|
136+
format.json do
137+
additional_metrics = environment.additional_metrics || {}
136138

137-
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
139+
render json: additional_metrics, status: additional_metrics.any? ? :ok : :no_content
140+
end
141+
end
138142
end
139143

140144
private

app/controllers/projects/prometheus_controller.rb

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ def active_metrics
1818

1919
private
2020

21-
rescue_from(ActionController::UnknownFormat) do
22-
render_404
23-
end
24-
2521
def require_prometheus_metrics!
2622
render_404 unless project.prometheus_service.present?
2723
end

spec/controllers/application_controller_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ def index
9999
end
100100
end
101101

102+
describe 'response format' do
103+
controller(described_class) do
104+
def index
105+
respond_to do |format|
106+
format.json do
107+
head :ok
108+
end
109+
end
110+
end
111+
end
112+
113+
context 'when format is handled' do
114+
let(:requested_format) { :json }
115+
116+
it 'returns 200 response' do
117+
get :index, private_token: user.private_token, format: requested_format
118+
119+
expect(response).to have_http_status 200
120+
end
121+
end
122+
123+
context 'when format is not handled' do
124+
it 'returns 404 response' do
125+
get :index, private_token: user.private_token
126+
127+
expect(response).to have_http_status 404
128+
end
129+
end
130+
end
131+
102132
describe '#authenticate_user_from_rss_token' do
103133
describe "authenticating a user from an RSS token" do
104134
controller(described_class) do

0 commit comments

Comments
 (0)