Sha256: 5e6079c73664afce316c449f8d9c54666d99c6c61b7aa681a3a4889f1c6ec789

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Ezcater
      # Checks for `raise` on `StandardError` and `ArgumentError`.
      # We want to be explicit about the error we're raising and use a custom error
      #
      # @example
      #   # bad
      #   raise StandardError, "You can't do that"
      #
      #   # good
      #   raise OrderActionNotAllowed

      class RequireCustomError < Base
        MSG = "Use a custom error class that inherits from StandardError when raising an exception"

        def_node_matcher :raising_standard_or_argument_error,
                         "(send nil? {:raise :fail} (const nil? {:StandardError :ArgumentError} ...) ...)"

        def_node_matcher :initializing_standard_or_argument_error,
                         "(send nil? {:raise :fail} (send (const nil? {:StandardError :ArgumentError} ...) ...))"

        def on_send(node)
          raising_standard_or_argument_error(node) do
            add_offense(node, message: format(MSG))
          end

          initializing_standard_or_argument_error(node) do
            add_offense(node, message: format(MSG))
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ezcater_rubocop-7.1.2 lib/rubocop/cop/ezcater/require_custom_error.rb
ezcater_rubocop-7.1.1 lib/rubocop/cop/ezcater/require_custom_error.rb
ezcater_rubocop-7.1.0 lib/rubocop/cop/ezcater/require_custom_error.rb