Module: LogStruct::ParamsErrors
- Extended by:
- T::Sig
- Defined in:
- lib/log_struct/params_errors.rb
Overview
Errors from parsing a request's params: Rails' ParseError (and its ParamErrors), and Rack's. Their messages, and
those of errors raised because of them (Rails' "Invalid request parameters: ..."), quote the request's input: a
JSON parser's excerpt of the body, Rack's "invalid %-encoding (
Constant Summary collapse
- NAMES =
Matched by name, so this works without ActionDispatch or Rack loaded
[ "ActionDispatch::Http::Parameters::ParseError", "Rack::BadRequest", "Rack::QueryParser::InvalidParameterError", "Rack::QueryParser::ParameterTypeError", "Rack::QueryParser::ParamsTooDeepError" ].freeze
- PARSE_ERROR_MESSAGE =
Rails' message for a ParseError
"Error occurred while parsing request parameters"- PARAMS_ERROR_MESSAGE =
"Invalid request parameters"- CAUSE_MESSAGE_LEFT_OUT =
"(left out: it quotes the request's input)"
Class Method Summary collapse
- .default_message?(error) ⇒ Boolean
-
.messages(error) ⇒ Array<(String, [String, nil])>
The message and the cause's message to log for an error, without the input a params parser quoted.
- .params_error?(error) ⇒ Boolean
- .parse_error?(error) ⇒ Boolean
-
.replacements(error) ⇒ Array<Array<(String, String)>>
For text that writes an error's whole chain (its causes too): each message in it that quotes a params parser's input, with what to write instead.
Class Method Details
.default_message?(error) ⇒ Boolean
60 61 62 |
# File 'lib/log_struct/params_errors.rb', line 60 def self.(error) error. == error.class.name end |
.messages(error) ⇒ Array<(String, [String, nil])>
The message and the cause's message to log for an error, without the input a params parser quoted
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/log_struct/params_errors.rb', line 27 def self.(error) cause = error.cause if params_error?(error) [parse_error?(error) ? PARSE_ERROR_MESSAGE : PARAMS_ERROR_MESSAGE, nil] elsif cause && params_error?(cause) = error..sub(cause., "").sub(/[\s:]+\z/, "") [.empty? ? PARAMS_ERROR_MESSAGE : , nil] else [error., cause&.] end end |
.params_error?(error) ⇒ Boolean
71 72 73 |
# File 'lib/log_struct/params_errors.rb', line 71 def self.params_error?(error) error.class.ancestors.any? { |ancestor| NAMES.include?(ancestor.name) } end |
.parse_error?(error) ⇒ Boolean
65 66 67 68 |
# File 'lib/log_struct/params_errors.rb', line 65 def self.parse_error?(error) !!defined?(::ActionDispatch::Http::Parameters::ParseError) && error.instance_of?(::ActionDispatch::Http::Parameters::ParseError) end |
.replacements(error) ⇒ Array<Array<(String, String)>>
For text that writes an error's whole chain (its causes too): each message in it that quotes a params parser's input, with what to write instead. Longest first, since an error's message can contain its cause's. An error raised without a message (Rack's and Rails' depth limits) has its class name as its message, which quotes nothing and is also the text's label for its class, so it stays.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/log_struct/params_errors.rb', line 44 def self.replacements(error) pairs = T.let([], T::Array[[String, String]]) seen = T.let({}.compare_by_identity, T::Hash[Exception, TrueClass]) current = T.let(error, T.nilable(Exception)) while current && !seen.key?(current) seen[current] = true , = (current) pairs << [current., ] if != current. && !(current) cause = current.cause pairs << [cause., CAUSE_MESSAGE_LEFT_OUT] if cause && .nil? && !(cause) current = cause end pairs.reject { |quoted, _| quoted.empty? }.uniq.sort_by { |quoted, _| -quoted.length } end |