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

Use Exception#message getter instead of @ivar to allow overloading #3194

Merged
merged 1 commit into from
Aug 30, 2016
Merged

Conversation

Sija
Copy link
Contributor

@Sija Sija commented Aug 25, 2016

Suggestion as in the title. It would match Ruby behavior too.

ruby -e "class FooError < RuntimeError; def message; super + ' -- bar!'; end; end" -e "raise FooError.new('foo?')"
-e:2:in '<main>': foo? -- bar! (FooError)

@luislavena
Copy link
Contributor

@Sija would you mind adding a spec that test customization of message and checks it?

Thank you.

@Sija
Copy link
Contributor Author

Sija commented Aug 25, 2016

@luislavena Sure thing! I don't understand tho why there's nil assertion failing, could you have a look, please?

@bcardiff
Copy link
Member

I found what's going on. In b3943d5#diff-8a92cd1599650bc7c991a21c66fbc77bR171 there should be backtrace?.try &.each do and not backtrace.try &.each do .

There was not specs for Exception#inspect_with_backtrace so that's why it didn't appear before. So this is really useful.

I will do one more comment inline and after squashing the commits in a single commit we should be fine :-).

require "spec"

class FooError < Exception
def message
Copy link
Member

Choose a reason for hiding this comment

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

Due to how you use the specs I will suggest

class FooError < Exception
  def message
    "#{super || ""} -- bar!"
  end
end

So that the spec tests that the base message implementation returns the value used at creation and that to_s / inspect_with_backtrace use the method rather than the ivar.

find a full sample at https://play.crystal-lang.org/#/r/17lx

@bcardiff
Copy link
Member

@Sija if you may, include a fix for #3194 (comment) so the specs pass.

Then if you don't know how to squash the PR in a single commit check here.

Thanks!

@Sija
Copy link
Contributor Author

Sija commented Aug 25, 2016

@bcardiff Since specs are still failing should I include the fix for Exception#inspect_with_backtrace inside the exception_spec.cr?

@bcardiff
Copy link
Member

@Sija yes please. I tried to be clear on that, sorry (& thanks!).
And the changes in a single commit should be fine. I still see that you are using 1 for the fix and 1 for the specs.

@Sija
Copy link
Contributor Author

Sija commented Aug 25, 2016

@bcardiff Oki doki, I wasn't sure I grok your intention ;) Should be GTG now.

class Exception
def inspect_with_backtrace(io : IO)
io << message << " (" << self.class << ")\n"
backtrace?.try &.each do |frame|
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't be better this fix is part of the code itself (src/exception.cr) and not just the specs? /cc @bcardiff

Copy link
Member

Choose a reason for hiding this comment

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

Definitely. @Sija please add the ? exception.cr and remove the Exception class in exception_spec.cr, otherwise the code that is been tested is not the actual code. I made the shared snippet to illustrate and quick check if the error was the missing ?.

@Sija
Copy link
Contributor Author

Sija commented Aug 30, 2016

@bcardiff done.

@bcardiff bcardiff merged commit ee730f8 into crystal-lang:master Aug 30, 2016
@bcardiff
Copy link
Member

Thanks!

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.

None yet

4 participants