Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify config handling & template lookup #130

Merged
merged 7 commits into from
Mar 5, 2019

Conversation

timriley
Copy link
Member

@timriley timriley commented Mar 3, 2019

Centralize validation of view class-level config, and along with the error that we raise for a not-configured template, raise another error if the paths are not configured.

Clarify template lookup:

  • Don't attempt to look in parent dirs when rendering templates
  • Only look in shared/ subdirs when rendering partials.

Refactor template lookup code so it's easier to follow. Add extra values to the result cache key to avoid incorrect cache hits.

Move TemplateNotFoundError into main Dry::View namespace, since it's likely something that will be be encountered by users, and they don't need to see the nesting within Dry::View::Renderer.

lib/dry/view.rb Outdated
# @api private
def ensure_config
raise UndefinedPathsError.new unless Array(config.paths).any?
raise UndefinedTemplateError.new unless config.template
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solnic @flash-gordon Style question for you. Which would you consider better:

  • Different error classes for each bit of config that is critical to rendering, like we have here, or
  • A single InvalidConfig error that we raise with different messages based on which piece of config is invalid?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha Actually, this might be good to ask you, too. Would you rather have a single class for config-related errors, or an individual class for each specific config key that could be invalid?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timriley No strong preference here. As a general rule that I follow, it's worth to have specific error objects, if you need to enrich them with debug information.


For instance the error UndefinedPathsError can be useful if raised with configured paths:

raise UndefinedPathsError.new(config.paths) unless Array(config.paths).any?

So devs will see an informative error message:

Dry::View::UndefinedPathsError: cannot find paths: `path/to/missing/directory'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jodosha Thanks for the feedback :) In this case I decided to unify the errors and pass through the missing configuration key as the extra data:

      raise UndefinedConfigError.new(:paths) unless Array(config.paths).any?
      raise UndefinedConfigError.new(:template) unless config.template

@timriley timriley changed the title Clarify template lookup Clarify config handling & template lookup Mar 3, 2019
@timriley
Copy link
Member Author

timriley commented Mar 3, 2019

This is ready to merge, just leaving this open so I can get @solnic / @flash-gordon's feedback on the error classes thing.

@timriley timriley merged commit 82cf27b into master Mar 5, 2019
@timriley timriley deleted the refactor/clarify-template-lookup branch March 5, 2019 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants