• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ruby-grape / grape / 10070087966

24 Jul 2024 03:37AM UTC coverage: 97.571% (-0.02%) from 97.595%
10070087966

push

github

web-flow
Fix rescue_from ValidationErrors exception (#2480)

* Fix rescue_from ValidationErrors exception

* Update CHANGELOG

9 of 9 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

3575 of 3664 relevant lines covered (97.57%)

67370.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/lib/grape/exceptions/validation_errors.rb
1
# frozen_string_literal: true
2

3
module Grape
49✔
4
  module Exceptions
49✔
5
    class ValidationErrors < Grape::Exceptions::Base
49✔
6
      ERRORS_FORMAT_KEY = 'grape.errors.format'
49✔
7
      DEFAULT_ERRORS_FORMAT = '%<attributes>s %<message>s'
49✔
8

9
      include Enumerable
49✔
10

11
      attr_reader :errors
49✔
12

13
      def initialize(errors: [], headers: {}, **_options)
49✔
14
        @errors = errors.group_by(&:params)
13,806✔
15
        super(message: full_messages.join(', '), status: 400, headers: headers)
13,806✔
16
      end
17

18
      def each
49✔
19
        errors.each_pair do |attribute, errors|
14,046✔
20
          errors.each do |error|
17,128✔
21
            yield attribute, error
18,168✔
22
          end
23
        end
24
      end
25

26
      def as_json(**_opts)
49✔
27
        errors.map do |k, v|
80✔
28
          {
29
            params: k,
80✔
30
            messages: v.map(&:to_s)
31
          }
32
        end
33
      end
34

35
      def to_json(*_opts)
49✔
UNCOV
36
        as_json.to_json
×
37
      end
38

39
      def full_messages
49✔
40
        messages = map do |attributes, error|
13,886✔
41
          I18n.t(
18,008✔
42
            ERRORS_FORMAT_KEY,
43
            default: DEFAULT_ERRORS_FORMAT,
44
            attributes: translate_attributes(attributes),
45
            message: error.message
46
          )
47
        end
48
        messages.uniq!
13,886✔
49
        messages
13,886✔
50
      end
51
    end
52
  end
53
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc