🗂

[bugs.ruby][Feature #20164] Exception をパターンマッチに対応させる提案

2024/03/01に公開

[Feature #20164] Add Exception#deconstruct_keys

  • Exception#deconstruct_keys を追加して Exception に対してパターンマッチに対応させる提案
begin
  #code
rescue => err
  case err
  in StandardError(message: /Permission denied/)
    abort "please select a different file"
  in NameError(name: :foo)
    retry if require "foo_helper"
  else
    raise
  end
end
Exception                                [:backtrace, :backtrace_locations, :cause, :message]
StopIteration                            [:result]
FrozenError                              [:receiver]
SignalException                          [:signo, :signm]
KeyError                                 [:receiver, :key]
LoadError                                [:path]
LocalJumpError                           [:exit_value, :reason]
NameError                                [:name, :receiver, :local_variables]
NoMatchingPatternKeyError                [:key, :matchee]
NoMethodError                            [:args, :private_call?]
SyntaxError                              [:path]
SystemCallError                          [:errno]
SystemExit                               [:status, :success?]
UncaughtThrowError                       [:tag, :value]
Encoding::InvalidByteSequenceError       [:readagain_bytes, :source_encoding, :source_encoding_name, :incomplete_input?, :destination_encoding, :destination_encoding_name, :error_bytes]
Encoding::UndefinedConversionError       [:error_char, :source_encoding, :source_encoding_name, :destination_encoding, :destination_encoding_name]
OptionParser::ParseError                 [:args, :reason, :additional]
Ractor::RemoteError                      [:ractor]
Timeout::Error                           [:thread]
RDoc::RI::Driver::NotFoundError          [:name]
RDoc::Store::MissingFileError            [:name, :file, :store]
Reline::Config::InvalidInputrc           [:file, :lineno]
Gem::LoadError                           [:name, :requirement]
Gem::ConflictError                       [:target, :conflicts]
Gem::DependencyResolutionError           [:conflict, :conflicting_dependencies]
Gem::FilePermissionError                 [:directory]
Gem::FormatException                     [:file_path]
Gem::GemNotInHomeException               [:spec]
Gem::ImpossibleDependenciesError         [:request, :build_message, :dependency, :conflicts]
Gem::MissingSpecVersionError             [:specs]
Gem::RuntimeRequirementNotMetError       [:suggestion]
Gem::SpecificGemNotFoundException        [:name, :errors, :version]
Gem::SystemExitException                 [:exit_code]
Gem::UninstallError                      [:spec]
Gem::UnknownCommandError                 [:unknown_command]
Gem::UnsatisfiableDependencyError        [:name, :dependency, :errors, :version]
Gem::RequestSet::Lockfile::ParseError    [:column, :path, :line]
Gem::Resolver::Molinillo::CircularDependencyError [:dependencies]
Gem::Resolver::Molinillo::NoSuchDependencyError [:dependency, :required_by]
Gem::Resolver::Molinillo::VersionConflict [:specification_provider, :conflicts]
  • これはまあ普通に便利そうな気がする
GitHubで編集を提案

Discussion