Skip to content

Commit 918ad55

Browse files
committed
refactored the full_messages method
with this I'm getting rid of `full_message` method which in ruby 2.5 returns a formatted string of the error. https://ruby-doc.org/core-2.5.0/Exception.html#full_message-method
1 parent b9d53a2 commit 918ad55

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* [#1976](https://github.com/ruby-grape/grape/pull/1976): Ensure classes/modules listed for autoload really exist - [@dnesteryuk](https://github.com/dnesteryuk).
1212
* [#1971](https://github.com/ruby-grape/grape/pull/1971): Fix BigDecimal coercion - [@FlickStuart](https://github.com/FlickStuart).
1313
* [#1968](https://github.com/ruby-grape/grape/pull/1968): Fix args forwarding in Grape::Middleware::Stack#merge_with for ruby 2.7.0 - [@dm1try](https://github.com/dm1try).
14-
14+
* [#1988](https://github.com/ruby-grape/grape/pull/1988): Refactored the full_messages method and stop overriding full_message - [@hosseintoussi](https://github.com/hosseintoussi).
1515
### 1.3.0 (2020/01/11)
1616

1717
#### Features

lib/grape/exceptions/base.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ def translate_attributes(keys, **options)
5757
end.join(', ')
5858
end
5959

60-
def translate_attribute(key, **options)
61-
translate("#{BASE_ATTRIBUTES_KEY}.#{key}", default: key, **options)
62-
end
63-
6460
def translate_message(key, **options)
6561
case key
6662
when Symbol

lib/grape/exceptions/validation_errors.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
module Grape
66
module Exceptions
77
class ValidationErrors < Grape::Exceptions::Base
8+
ERRORS_FORMAT_KEY = 'grape.errors.format'
9+
DEFAULT_ERRORS_FORMAT = '%{attributes} %{message}'
10+
811
include Enumerable
912

1013
attr_reader :errors
@@ -41,21 +44,17 @@ def to_json(**_opts)
4144
end
4245

4346
def full_messages
44-
messages = map { |attributes, error| full_message(attributes, error) }
47+
messages = map do |attributes, error|
48+
I18n.t(
49+
ERRORS_FORMAT_KEY,
50+
default: DEFAULT_ERRORS_FORMAT,
51+
attributes: translate_attributes(attributes),
52+
message: error.message
53+
)
54+
end
4555
messages.uniq!
4656
messages
4757
end
48-
49-
private
50-
51-
def full_message(attributes, error)
52-
I18n.t(
53-
'grape.errors.format',
54-
default: '%{attributes} %{message}',
55-
attributes: attributes.count == 1 ? translate_attribute(attributes.first) : translate_attributes(attributes),
56-
message: error.message
57-
)
58-
end
5958
end
6059
end
6160
end

0 commit comments

Comments
 (0)