diff --git a/app/controllers/alchemy/admin/base_controller.rb b/app/controllers/alchemy/admin/base_controller.rb index c4b6bab458..e36f133e67 100644 --- a/app/controllers/alchemy/admin/base_controller.rb +++ b/app/controllers/alchemy/admin/base_controller.rb @@ -48,14 +48,13 @@ def show_error_notice(error) @error = error # truncate the message, because very long error messages (i.e from mysql2) causes cookie overflow errors @notice = error.message[0..255] - @trace = error.backtrace[0..50] - if request.xhr? + if request.format.json? + render json: {message: @notice}, status: 500 + elsif request.xhr? render action: "error_notice" else - respond_to do |format| - format.html { render "500", status: 500 } - format.json { render json: {message: @notice}, status: 500 } - end + @trace = error.backtrace[0..50] + render "500", status: 500 end end diff --git a/spec/controllers/alchemy/admin/base_controller_spec.rb b/spec/controllers/alchemy/admin/base_controller_spec.rb index bd34afbf94..0cc4e51de9 100644 --- a/spec/controllers/alchemy/admin/base_controller_spec.rb +++ b/spec/controllers/alchemy/admin/base_controller_spec.rb @@ -93,6 +93,63 @@ end end + describe "#show_error_notice" do + let(:error) do + ActiveRecord::ActiveRecordError.new("Database is busy") + end + + subject do + controller.send(:show_error_notice, error) + end + + before do + allow(controller).to receive(:render) + end + + context "for a json request" do + before do + expect(controller).to receive(:request) do + double(format: double(json?: true)) + end + end + + it "returns error message" do + subject + expect(controller).to have_received(:render).with( + json: {message: "Database is busy"}, + status: 500 + ) + end + end + + context "for a xhr request" do + before do + expect(controller).to receive(:request) do + double(xhr?: true, format: double(json?: false)) + end.twice + end + + it "renders error notice" do + subject + expect(controller).to have_received(:render).with(action: "error_notice") + end + end + + context "for a html request" do + before do + expect(controller).to receive(:request) do + double(xhr?: false, format: double(json?: false)) + end.twice + error.set_backtrace(%(foo)) + end + + it "renders error template" do + subject + expect(controller).to have_received(:render).with("500", status: 500) + end + end + end + context "when current_alchemy_user is present" do let!(:page_1) { create(:alchemy_page, name: "Page 1") } let!(:page_2) { create(:alchemy_page, name: "Page 2") }