Skip to content

Custom error classes and respond_to_missing? #166

Description

@eitoball

Summary

Replace string raises with structured error classes and add respond_to_missing? to the extractors for better debugging and extension.

Problem

Opaque string errors

Errors today are plain strings:

raise "No comma format for class #{self.class} defined for style #{style}"
raise "Unknown data symbol #{arg.inspect}"
raise "Unknown header symbol #{arg.inspect}"

Callers cannot rescue Comma::UnknownStyle programmatically. Messages are fine for humans but fragile for apps that want to handle missing formats gracefully.

Locations:

  • lib/comma/object.rb
  • lib/comma/data_extractor.rb
  • lib/comma/header_extractor.rb

Missing respond_to_missing?

Both extractors use method_missing without respond_to_missing?. RuboCop excludes this via .rubocop_todo.yml (Style/MissingRespondToMissing).

That breaks expected Ruby semantics for introspection (respond_to?, IRB, some tooling).

Proposed approach

Define error hierarchy under Comma:

module Comma
  class Error < StandardError; end
  class UnknownStyle < Error; end
  class UnknownColumnArgument < Error; end
  class CircularStyleReference < Error; end  # pairs with #1
end

Raise these with messages matching or improving current strings (backward compatible for string-matching rescues).

Add respond_to_missing? on both extractors delegating to the same rules as method_missing (always true for DSL method names inside a comma block context, or mirror method_missing behavior).

Files likely involved

  • New: lib/comma/errors.rb
  • lib/comma/object.rb
  • lib/comma/data_extractor.rb
  • lib/comma/header_extractor.rb
  • lib/comma/extractor.rb
  • Specs asserting error type (can keep message assertions too)

Acceptance criteria

  • Missing style raises Comma::UnknownStyle (message still mentions class and style)
  • Invalid column args raise Comma::UnknownColumnArgument
  • respond_to_missing? implemented on both extractors
  • Existing specs that expect RuntimeError or message strings updated intentionally (document in PR if message-only rescues exist in the wild)
  • RuboCop todo entry for MissingRespondToMissing can be removed for extractor files

Labels (suggested)

refactor, enhancement

Depends on

Optional: #1 (CircularStyleReference error class)

Notes

Consider documenting rescuable errors in README/wiki in a follow-up; not required for this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions