Allow `NotFound` to specify an optional `Location` for the resource

This commit is contained in:
Robin Ward 2017-09-26 09:09:48 -04:00
parent 367fb1c524
commit 4ae66c9e01
2 changed files with 25 additions and 3 deletions

View File

@ -136,7 +136,15 @@ class ApplicationController < ActionController::Base
end
end
rescue_from Discourse::NotFound, PluginDisabled do
rescue_from Discourse::NotFound do |e|
rescue_discourse_actions(
:not_found,
404,
location: e.location
)
end
rescue_from PluginDisabled do |e|
rescue_discourse_actions(:not_found, 404)
end
@ -159,10 +167,18 @@ class ApplicationController < ActionController::Base
(request.xhr?) ||
((params[:external_id] || '').ends_with? '.json')
if opts[:location]
response.headers['Location'] = opts[:location]
end
if show_json_errors
# HACK: do not use render_json_error for topics#show
if request.params[:controller] == 'topics' && request.params[:action] == 'show'
return render status: status_code, layout: false, text: (status_code == 404 || status_code == 410) ? build_not_found_page(status_code) : I18n.t(type)
return render(
status: status_code,
layout: false,
text: (status_code == 404 || status_code == 410) ? build_not_found_page(status_code) : I18n.t(type)
)
end
render_json_error I18n.t(opts[:custom_message] || type), type: type, status: status_code

View File

@ -74,7 +74,13 @@ module Discourse
end
# When something they want is not found
class NotFound < StandardError; end
class NotFound < StandardError
attr_reader :location
def initialize(opts = nil)
opts ||= {}
@location = opts[:location]
end
end
# When a setting is missing
class SiteSettingMissing < StandardError; end