Localize error messages in API V3

pull/2282/head
Hagen Schink 10 years ago
parent 1aa6c806e3
commit f7e6a6c430
  1. 2
      lib/api/errors/conflict.rb
  2. 11
      lib/api/errors/form/invalid_resource_link.rb
  3. 2
      lib/api/errors/parse_error.rb
  4. 2
      lib/api/errors/unauthenticated.rb
  5. 2
      lib/api/errors/unauthorized.rb
  6. 8
      lib/api/errors/unwritable_property.rb
  7. 5
      lib/api/errors/validation.rb
  8. 16
      lib/api/v3/render/render_api.rb

@ -31,7 +31,7 @@ module API
module Errors
class Conflict < ErrorBase
def initialize
super 409, 'Couldn\'t update the resource because of conflicting modifications.'
super 409, I18n.t('api_v3.errors.code_409')
end
end
end

@ -32,10 +32,13 @@ module API
module Form
class InvalidResourceLink < StandardError
def initialize(property, expected_resource, actual_resource = :unknown)
expected = expected_resource.to_s.singularize.capitalize
actual = actual_resource.to_s.singularize.capitalize
message = "For property #{property} a resource of type #{expected}" \
" is expected but got a resource of type #{actual}"
property_localized = I18n.t("attributes.#{property}")
expected_localized = I18n.t("attributes.#{expected_resource.to_s.singularize}")
actual_localized = I18n.t("attributes.#{actual_resource.to_s.singularize}")
message = I18n.t('api_v3.errors.invalid_resource',
property: property_localized,
expected: expected_localized,
actual: actual_localized)
super(message)
end

@ -31,7 +31,7 @@ module API
module Errors
class ParseError < InvalidRequestBody
def initialize(message)
super 'The request body was neither empty, nor did it contain a single JSON object.'
super I18n.t('api_v3.errors.parse_error')
@details = { parseError: clean_parse_error(message) }
end

@ -31,7 +31,7 @@ module API
module Errors
class Unauthenticated < ErrorBase
def initialize
super 401, 'You need to be authenticated to access this resource.'
super 401, I18n.t('api_v3.errors.code_401')
end
end
end

@ -31,7 +31,7 @@ module API
module Errors
class Unauthorized < ErrorBase
def initialize
super 403, 'You are not authorized to access this resource.'
super 403, I18n.t('api_v3.errors.code_403')
end
end
end

@ -35,7 +35,13 @@ module API
fail ArgumentError, 'UnwritableProperty error must contain at least one invalid attribute!' if attributes.empty?
super 422, 'You must not write a read-only attribute.'
if attributes.length == 1
message = I18n.t('api_v3.errors.writing_read_only_attributes')
else
message = I18n.t('api_v3.errors.multiple_errors')
end
super 422, message
if attributes.length > 1
invalid_attributes.each do |attribute|

@ -32,8 +32,11 @@ module API
class Validation < ErrorBase
def initialize(obj)
messages = obj.respond_to?(:errors) ? obj.errors.full_messages : Array(obj)
message = I18n.t('api_v3.errors.multiple_errors')
super 422, (messages.length == 1) ? messages[0] + '.' : 'Multiple fields violated their constraints.'
message = messages[0] + '.' if messages.length == 1
super 422, message
messages.each { |m| @errors << Validation.new(m) } if messages.length > 1
end

@ -43,7 +43,9 @@ module API
actual = request.content_type
unless actual.starts_with? SUPPORTED_MEDIA_TYPE
message = "Expected content type '#{SUPPORTED_MEDIA_TYPE}' but got '#{actual}'."
message = I18n.t('api_v3.errors.invalid_content_type',
content_type: SUPPORTED_MEDIA_TYPE,
actual: actual)
fail API::Errors::InvalidRequestBody, message
end
@ -61,7 +63,9 @@ module API
def context_object
try_context_object
rescue ::ActiveRecord::RecordNotFound
fail API::Errors::InvalidRenderContext.new('Context does not exist!')
fail API::Errors::InvalidRenderContext.new(
I18n.t('api_v3.errors.render.context_object_not_found')
)
end
def try_context_object
@ -79,9 +83,13 @@ module API
context = ::API::V3::Utilities::ResourceLinkParser.parse(params[:context])
if context.nil?
fail API::Errors::InvalidRenderContext.new('No context found.')
fail API::Errors::InvalidRenderContext.new(
I18n.t('api_v3.errors.render.context_not_found')
)
elsif !SUPPORTED_CONTEXT_NAMESPACES.include? context[:ns]
fail API::Errors::InvalidRenderContext.new('Unsupported context found.')
fail API::Errors::InvalidRenderContext.new(
I18n.t('api_v3.errors.render.unsupported_context')
)
else
context
end

Loading…
Cancel
Save