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

Make yaml loading error more transparent to the end user #13

Closed
wants to merge 3 commits into from
Closed

Make yaml loading error more transparent to the end user #13

wants to merge 3 commits into from

Conversation

purbon
Copy link

@purbon purbon commented Oct 9, 2015

Fix #5

@@ -137,18 +137,18 @@ def load_yaml(registering=false)

begin
@dictionary.merge!(YAML.load_file(@dictionary_path))
rescue Exception => e

Choose a reason for hiding this comment

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

There is an understandable but flawed reason why this line was added. It is because YAML (on jruby, == Psych) defines SyntaxError as a subclass of RuntimeError not StandardError, so hence the rescue Exception => e

This is a classic case of the discussion that @jsvd and I raised about plugins handling specific errors in their rescue blocks. See this discussion issue

The work that may raise an Error in line 139 is doing many things, opening and parsing a file then merging the results to a Hash.

There should be two rescue sections.

rescue YAML::SyntaxError => e
  msg = "..." #specific to the syntax problem
  raise Exception.new(msg) #this will be thrown up to the pipeline; v2.0 should rescue, attempt to log it and reraise, this will kill the worker thread
rescue StandardError => e
  msg = "..." #general message
  #log error

Copy link
Author

Choose a reason for hiding this comment

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

I know SyntaxError is a RuntimeError, because in ruby is a ruby syntax error so it happen in runtime, not an error type located for things like YAML, JSON, or XML syntax validation.

The question I ask myself here is, the actual behaviour is if registering is set to true, raise the exception otherwise not. If we aim to keep this, adding YAML::SyntaxError provide us only with a way to detect when this is a syntax error or not. I understand we don't want to react different for SyntaxError and other errors (StandardError). In your example you aim to raise only SyntaxException up, and log everything under StandardError.

I understand the issue as not reporting everything as syntax error even when they are not, in my example user will notice when Psych claims an error, and also when there are other error.

What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

@guyboertje follow up on that?

Choose a reason for hiding this comment

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

But with rescue => e on new line 140, you are only handling StandardError not SyntaxError or any other non-StandardError that might be thrown. Also this StandardError is being converted to a RuntimeError and re-raised - if the caller, in its rescue blocks, makes choices based on the class then the choices may be wrong.

If your aim is to add extra info to the error message, then consider this alternative:

  rescue Exception => e
    msg = ...
    new_error = e.class.new(msg)
    if registering
      raise new_error
    else
      @logger.warn("#{new_error.message}, continuing ..")
    end
  end

@purbon
Copy link
Author

purbon commented Dec 2, 2015

@guyboertje thoughts on this PR? what do you think? good? any concern? thanks a lot for your time.

@purbon
Copy link
Author

purbon commented Dec 3, 2015

@guyboertje feedback on this?

if registering
raise "#{self.class.name}: Bad Syntax in dictionary file #{@dictionary_path}"
raise RuntimeError.new(msg)

Choose a reason for hiding this comment

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

I seems to me that the original exception may have info about where (line, column) the syntax problem is. And in a big YAML file that info is gold.

Consider, trying to extract that from the original error and logging an error message especially during register.

Choose a reason for hiding this comment

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

If you do implement my suggestion, consider changing the YAML fixture to be a lengthy (10 lines) one that have a proper syntax error and verify that the error message logged shows it.

Copy link
Author

Choose a reason for hiding this comment

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

this information is already included in the message coming out of the exception.

@guyboertje
Copy link

Other than one idea to consider - LGTM.

@elasticsearch-bot
Copy link

Pere Urbon merged this into the following branches!

Branch Commits
master d0ef4e1, 2b1fc61, 14f7b0e

@purbon purbon closed this in d0ef4e1 Dec 10, 2015
purbon pushed a commit that referenced this pull request Dec 10, 2015
purbon pushed a commit that referenced this pull request Dec 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants