Adds better reusable error message support. Added to fetching remote

posts. /cc @riking
This commit is contained in:
Robin Ward 2014-04-02 13:22:10 -04:00
parent b250aa36a0
commit 558a06a117
6 changed files with 87 additions and 5 deletions

View File

@ -223,6 +223,8 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
this.set('loadingExpanded', true); this.set('loadingExpanded', true);
post.expand().then(function() { post.expand().then(function() {
self.set('firstPostExpanded', true); self.set('firstPostExpanded', true);
}).catch(function(error) {
bootbox.alert($.parseJSON(error.responseText).errors);
}).finally(function() { }).finally(function() {
self.set('loadingExpanded', false); self.set('loadingExpanded', false);
}); });

View File

@ -5,10 +5,12 @@ require_dependency 'custom_renderer'
require_dependency 'archetype' require_dependency 'archetype'
require_dependency 'rate_limiter' require_dependency 'rate_limiter'
require_dependency 'crawler_detection' require_dependency 'crawler_detection'
require_dependency 'json_error'
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include CurrentUser include CurrentUser
include CanonicalURL::ControllerExtensions include CanonicalURL::ControllerExtensions
include JsonError
serialization_scope :guardian serialization_scope :guardian
@ -242,11 +244,7 @@ class ApplicationController < ActionController::Base
end end
def render_json_error(obj) def render_json_error(obj)
if obj.present? render json: MultiJson.dump(create_errors_json(obj)), status: 422
render json: MultiJson.dump(errors: obj.errors.full_messages), status: 422
else
render json: MultiJson.dump(errors: [I18n.t('js.generic_error')]), status: 422
end
end end
def success_json def success_json

View File

@ -151,6 +151,8 @@ class PostsController < ApplicationController
doc.content doc.content
end end
render json: {cooked: content} render json: {cooked: content}
rescue
render_json_error I18n.t('errors.embed.load_from_remote')
end end
def recover def recover

View File

@ -28,6 +28,10 @@ en:
via: "%{username} via %{site_name}" via: "%{username} via %{site_name}"
is_reserved: "is reserved" is_reserved: "is reserved"
errors:
embed:
load_from_remote: "There was an error loading that post."
backup: backup:
operation_already_running: "An operation is currently running. Can't start a new job right now." operation_already_running: "An operation is currently running. Can't start a new job right now."
backup_file_should_be_tar_gz: "The backup file should be a .tar.gz archive." backup_file_should_be_tar_gz: "The backup file should be a .tar.gz archive."

21
lib/json_error.rb Normal file
View File

@ -0,0 +1,21 @@
module JsonError
def create_errors_json(obj)
# If we're passed a string, assume that is the error message
return {errors: [obj]} if obj.is_a?(String)
# If it looks like an activerecord object, extract its messages
return {errors: obj.errors.full_messages } if obj.respond_to?(:errors) && obj.errors.present?
# default to a generic error
JsonError.generic_error
end
private
def self.generic_error
{errors: [I18n.t('js.generic_error')]}
end
end

View File

@ -0,0 +1,55 @@
require 'spec_helper'
require_dependency 'json_error'
shared_examples "a generic error" do
let(:result) { creator.create_errors_json(obj) }
it "should have a result object" do
result.should be_present
end
it "has a generic error message" do
result[:errors].should == [I18n.t('js.generic_error')]
end
end
describe JsonError do
let(:creator) { Object.new.extend(JsonError) }
describe "with a nil argument" do
it_behaves_like "a generic error" do
let(:obj) { nil }
end
end
describe "with an empty array" do
it_behaves_like "a generic error" do
let(:obj) { [] }
end
end
describe "with an activerecord object with no errors" do
it_behaves_like "a generic error" do
let(:obj) { Fabricate.build(:user) }
end
end
describe "with a string" do
it "returns the string in the error format" do
creator.create_errors_json("test error").should == {errors: ["test error"]}
end
end
describe "an activerecord objec with errors" do
let(:invalid_user) { User.new }
it "returns the errors correctly" do
invalid_user.should_not be_valid
result = creator.create_errors_json(invalid_user)
result.should be_present
result[:errors].should_not be_blank
end
end
end