Move app/labor/error_message_cleaner.rb to service (#11679)

This commit is contained in:
Michael Kohl 2020-12-02 11:05:36 +07:00 committed by GitHub
parent 195e228307
commit c5a29826e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 18 deletions

View file

@ -88,7 +88,7 @@ class ArticlesController < ApplicationController
processed_html = parsed_markdown.finalize
rescue StandardError => e
@article = Article.new(body_markdown: params[:article_body])
@article.errors[:base] << ErrorMessageCleaner.new(e.message).clean
@article.errors[:base] << ErrorMessages::Clean.call(e.message)
end
respond_to do |format|

View file

@ -1,16 +0,0 @@
class ErrorMessageCleaner
attr_accessor :error_message
def initialize(error_message)
@error_message = error_message
end
def clean
if error_message.include?("expected key while parsing a block mapping at line")
"There was a problem parsing the front-matter YAML. Perhaps you need to escape a quote " \
"or a colon or something. Email #{SiteConfig.email_addresses[:contact]} if you are having trouble."
else
error_message
end
end
end

View file

@ -427,7 +427,7 @@ class Article < ApplicationRecord
self.description = processed_description if description.blank?
rescue StandardError => e
errors[:base] << ErrorMessageCleaner.new(e.message).clean
errors[:base] << ErrorMessages::Clean.call(e.message)
end
def set_tag_list(tags)

View file

@ -0,0 +1,15 @@
module ErrorMessages
class Clean
FRONTMATTER_ERROR = /expected key while parsing a block mapping at line/.freeze
REPLACEMENT_ERROR = "There was a problem parsing the front-matter YAML. " \
"Perhaps you need to escape a quote or a colon or something. " \
"Email %s if you are having trouble.".freeze
def self.call(error_message)
return error_message unless error_message.match?(FRONTMATTER_ERROR)
format(REPLACEMENT_ERROR, SiteConfig.email_addresses[:contact])
end
end
end