Skip to content

[MODULES-2462] Improve parseyaml function #511

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

Merged
merged 1 commit into from
Sep 14, 2015

Conversation

dmitryilyin
Copy link

  • Add default value support
    Second argument will be returned if yaml cannot be parsed
    instead of false value
  • Update tests

@DavidS
Copy link
Contributor

DavidS commented Aug 24, 2015

This is a continuation of #509. Please use "git push -f" to the PR's branch to keep the discussion in one place. We can continue here.

Having a default value in case of parse errors is indeed something pick* cannot provide. parseyaml("---\n*8", 'default value') will still error out, as there is no exception handling for the call in the function. I guess that's not what you intended. Add this spec:

it { is_expected.to run.with_params("---\n*8", 'default_value').and_return('default_value') }

to see this.

@dmitryilyin dmitryilyin force-pushed the parseyaml_default branch 5 times, most recently from f2249f2 to eb948c4 Compare August 25, 2015 18:40
* Add default value support
  Second argument will be returned if yaml cannot be parsed
  instead of false value
* Update tests
@dmitryilyin
Copy link
Author

Updated the function and spec. Also added same changes for parsejson function.

@holser
Copy link

holser commented Sep 3, 2015

+1

1 similar comment
@degorenko
Copy link

+1

@bmjen
Copy link
Contributor

bmjen commented Sep 14, 2015

👍 Thanks for the contribution and updates @dmitryilyin !

bmjen added a commit that referenced this pull request Sep 14, 2015
[MODULES-2462] Improve parseyaml function
@bmjen bmjen merged commit c0df819 into puppetlabs:master Sep 14, 2015
@dmitryilyin dmitryilyin deleted the parseyaml_default branch September 16, 2015 17:57
underscorgan pushed a commit to underscorgan/puppetlabs-stdlib that referenced this pull request Sep 21, 2015
Maintain the old behavior in the case where the optional second
parameter isn't passed. Also, adding arity is backwards incompatible since
stdlib still supports 2.7, so remove that.
bmjen added a commit that referenced this pull request Sep 21, 2015
YAML::load(arguments[0])
begin
YAML::load(arguments[0]) || arguments[1]
rescue Exception
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, er, I realize this is merged by now. But this is a problem.

Don't ever rescue Exception.

Thoughts? This seems to be a recurring issue in stdlib, yes? Should I open a ticket for this?

Choose a reason for hiding this comment

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

setup rubocop, profit :P

Copy link
Contributor

Choose a reason for hiding this comment

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

both, please. weeps

Copy link
Contributor

Choose a reason for hiding this comment

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

mumbles something about modulesync

@dmitryilyin
Copy link
Author

In this case rescuing exception is appropriate because I really want to rescue every possible situation and return the default value. But I guess I can gather a list of exceptions the parser can raise and rescue only them.

@ripienaar
Copy link

@dmitryilyin no you really cannot handle every possible exception. You wont handle NoMemoryError, SignalException, fatal, and probably shouldnt mask things like SyntaxError from the user

@dmitryilyin
Copy link
Author

Hmm, looks like there is quite a number of possible exceptions that can be raised by YAML module. There is a set of Psych exceptions, there can be Syck exceptions, there are C implementation of YAML loader and a number of Type errors and even Standard errors.
I don't this we really need all these listed here, and, to make things worse, more implementations and exceptions can be added without us knowing it.

So, because we are working with external module without a constant set of possible raised exceptions and we do want to rescue as much exceptions as possible, including Interrupt, Syntax error and even NoMemory error, it's appropriate to use anti-pattern of rescuing all exceptions here.

@ffrank
Copy link
Contributor

ffrank commented Mar 29, 2016

FWIW I opened #586 to track this.

The plethora of exceptions from (third party?) modules should inherit StandardError rather than plain Exception. If they do not, then this is either the module author's fault, or there is a good reason for making those hard to catch.

Also no, why would we want to rescue things like Interrupt? If I hit Ctrl-C at the wrong time, the process continues but uses a default value instead of some YAML data, in a way that does not leave so much as a log trace? That's bad UX, and a bug report waiting to happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants