-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvalidation.rb
More file actions
34 lines (29 loc) · 1.07 KB
/
validation.rb
File metadata and controls
34 lines (29 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# frozen_string_literal: true
module Grape
module Exceptions
class Validation < Base
EMPTY_BACKTRACE = [].freeze
attr_reader :params, :message_key
def initialize(params:, message: nil, status: nil, headers: nil)
@params = Array(params)
if message
@message_key = case message
when Symbol then message
when Hash then message[:key]
end
message = translate_message(message)
end
super(status:, message:, headers:)
# Pre-seed the backtrace so Ruby's raise skips capture. Validation errors are
# a hot path (raised per bad attribute) and end up as 400 Bad Request responses;
# backtraces here point into Grape internals and have no diagnostic value.
set_backtrace(EMPTY_BACKTRACE)
end
# Remove all the unnecessary stuff from Grape::Exceptions::Base like status
# and headers when converting a validation error to json or string
def as_json(*_args)
to_s
end
end
end
end