FIX: Don't give error 500 when invalid date param is given to admin reports (#8658)

Providing invalid dates as the end_date or start_date param causes a 500 error and creates noise in the logs. This will handle the error and returns a proper 400 response to the client with a message that explains what the problem is.
This commit is contained in:
Osama Sayegh 2020-01-03 17:01:38 +03:00 committed by GitHub
parent a8ffb6949c
commit fac71da605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -88,8 +88,12 @@ class Admin::ReportsController < Admin::AdminController
private private
def parse_params(report_params) def parse_params(report_params)
start_date = (report_params[:start_date].present? ? Time.parse(report_params[:start_date]).to_date : 1.days.ago).beginning_of_day begin
end_date = (report_params[:end_date].present? ? Time.parse(report_params[:end_date]).to_date : start_date + 30.days).end_of_day start_date = (report_params[:start_date].present? ? Time.parse(report_params[:start_date]).to_date : 1.days.ago).beginning_of_day
end_date = (report_params[:end_date].present? ? Time.parse(report_params[:end_date]).to_date : start_date + 30.days).end_of_day
rescue ArgumentError => e
raise Discourse::InvalidParameters.new(e.message)
end
facets = nil facets = nil
if Array === report_params[:facets] if Array === report_params[:facets]

View File

@ -47,6 +47,24 @@ describe Admin::ReportsController do
expect(JSON.parse(response.body)["reports"][1]["type"]).to eq("not_found") expect(JSON.parse(response.body)["reports"][1]["type"]).to eq("not_found")
end end
end end
context "invalid start or end dates" do
it "doesn't return 500 error" do
get "/admin/reports/bulk.json", params: {
reports: {
topics: { limit: 10, start_date: "2015-0-1" }
}
}
expect(response.status).to eq(400)
get "/admin/reports/bulk.json", params: {
reports: {
topics: { limit: 10, end_date: "2015-0-1" }
}
}
expect(response.status).to eq(400)
end
end
end end
end end