0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/lib/json_error.rb
Kris 24278059d2
UX: Link to required settings from language switcher validation error (#38374)
Currently the `content_localization_language_switcher` validation error
names prerequisite settings as plain text so admins have to search for
each one manually.

This change extends the `{{setting:...}}` marker from #40338 with a
multi-setting form: `{{settings:one,two|label}}`. This renders a single
link to a filtered view of all the referenced settings.

The error message now links each required setting inline plus an "All
required settings" link. The admin UI receives an HTML message (and
plain text is still provided elsewhere, like the API).

<img width="1408" height="464" alt="image"
src="https://github.com/user-attachments/assets/576bcd79-daf1-4bd7-84ec-2add4db27f85"
/>

All settings are shown with an OR search prefixed by `any:` so
pipe-delineated settings can still be searched for separately
`any:content_localization_language_switcher|set_locale_from_cookie|allow_user_locale|content_localization_supported_locales`

<img width="2318" height="1706" alt="image"
src="https://github.com/user-attachments/assets/332092de-8d7c-4935-9e08-c3cb38a38fad"
/>
2026-07-16 15:51:06 -04:00

47 lines
1.5 KiB
Ruby
Vendored

# frozen_string_literal: true
module JsonError
def create_errors_json(obj, opts = nil)
opts ||= {}
errors = create_errors_array obj
errors[:error_type] = opts[:type] if opts[:type]
errors[:extras] = opts[:extras] if opts[:extras]
errors[:html_message] = true if opts[:html_message]
errors
end
private
def create_errors_array(obj)
# If we're passed a string, assume that is the error message
return { errors: [obj] } if obj.is_a?(String)
# If it's an AR exception target the record
obj = obj.record if obj.is_a?(ActiveRecord::RecordInvalid)
# If it looks like an activerecord object, extract its messages
return { errors: obj.errors.full_messages } if obj.respond_to?(:errors) && obj.errors.present?
# If we're passed an array, it's an array of error messages
return { errors: obj.map(&:to_s) } if obj.is_a?(Array) && obj.present?
if obj.is_a?(Exception)
message = obj.cause.message.presence || obj.cause.class.name if obj.cause
message = obj.message.presence || obj.class.name if message.blank?
return { errors: [message] } if message.present?
end
return { errors: [I18n.t("not_found")] } if obj.is_a?(HasErrors) && obj.not_found
# Log a warning (unless obj is nil)
Rails.logger.warn("create_errors_json called with unrecognized type: #{obj.inspect}") if obj
# default to a generic error
JsonError.generic_error
end
def self.generic_error
{ errors: [I18n.t("js.generic_error")] }
end
end