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

BUGFIX: Fix bug where 'inspect_with_backtrace' raises an IndexError i… #625

Merged
merged 18 commits into from
Mar 2, 2018

Conversation

marksiemers
Copy link
Contributor

This is a bug in crystal maybe?

@faustinoaq
Copy link
Contributor

@marksiemers @eliasjpr Does this happen only on macOS?

I'm using Manjaro Linux and I haven't had this issue yet 😅

@elorest
Copy link
Member

elorest commented Feb 3, 2018

Both times my tests have failed because of it I have been using my living room iMac. It never used to fail before December though...

@marksiemers
Copy link
Contributor Author

@faustinoaq @elorest - Did it ever fail on travis?

@faustinoaq - either way, I think having a private method and a begin ... rescue is worth it.

In the case of a 500 error the overhead of this shouldn't really matter, response time should be a second priority to catching exception and error logging.

Additionally, for different environments (e.g. production) we probably do not want to render backtrace, so the private method here can expand to consider things like that.

I can and will change the puts statement to something else.

@marksiemers
Copy link
Contributor Author

As far as crystal goes, it looks like it has to do with here:
https://github.com/crystal-lang/crystal/blob/v0.24.1/src/exception.cr#L35

And therefore here:
https://github.com/crystal-lang/crystal/blob/351596855d635141deb204f7c572ed615f415ce5/src/callstack.cr#L157

And in callstack, it does do something different for macOS vs linux:
https://github.com/crystal-lang/crystal/blob/351596855d635141deb204f7c572ed615f415ce5/src/callstack.cr#L8

@marksiemers
Copy link
Contributor Author

Please don't delete this branch, unless you update the issue with crystal: crystal-lang/crystal#5678

Currently, this branch is referenced for reproducing a bug.

@elorest
Copy link
Member

elorest commented Feb 3, 2018

@faustinoaq @elorest - Did it ever fail on travis?@faustinoaq - either way, I think having a private method and a begin ... rescue is worth it.

I don't think it ever failed on travis (for this reason), but we obviously need the specs to pass locally for people running OSX as well. Also development will be weird if the message isn't returned. I would recommend returning the message from the rescue if it errors on inspect_with_backtrace, that way in development people will still see the error that was caused.

Similar to #626

# ex.backtrace? # or @ex.backtrace?
message = @ex.message
end
return message
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@elorest - The message from the original exception is returned in the case that inspect_with_backtrace raises an IndexError

The first priority is still @ex.inspect_with_backtrace and if that doesn't work, the second priority is @ex.message

line 39 runs in either case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Will this not work?

private def internal_server_error_message
  message = @ex.inspect_with_backtrace
rescue ex : IndexError
  # ERROR: Any of these will cause another IndexError (at least on macOS)
  # ex.inspect_with_backtrace # or @ex.inspect_with_backtrace
  # ex.backtrace? # or @ex.backtrace?
  @ex.message
end

Also it might be worth to use the flags just like in the crystal source code flag?(:freebsd) || flag?(:linux) || flag?(:openbsd) instead of rescueing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this should work:

private def internal_server_error_message
  @ex.inspect_with_backtrace
rescue ex : IndexError
  @ex.message
end

I prefer not to count on flags in this case. It is a bug in crystal, which has been resolved in master:
crystal-lang/crystal#5224
crystal-lang/crystal@5eecd57

Copy link
Contributor

@eliasjpr eliasjpr left a comment

Choose a reason for hiding this comment

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

I dont have a lot of context here. But left a comment based on my understanding.

# ex.backtrace? # or @ex.backtrace?
message = @ex.message
end
return message
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this not work?

private def internal_server_error_message
  message = @ex.inspect_with_backtrace
rescue ex : IndexError
  # ERROR: Any of these will cause another IndexError (at least on macOS)
  # ex.inspect_with_backtrace # or @ex.inspect_with_backtrace
  # ex.backtrace? # or @ex.backtrace?
  @ex.message
end

Also it might be worth to use the flags just like in the crystal source code flag?(:freebsd) || flag?(:linux) || flag?(:openbsd) instead of rescueing

@marksiemers
Copy link
Contributor Author

marksiemers commented Feb 5, 2018

It looks like a fix was merged for this issue: 055579f

Do we want this code with the private method, or leave it as is?
@elorest @eliasjpr

I like the idea of a private method because I think we will end up needing one anyway (to produce different messages based on the environment).

eliasjpr
eliasjpr previously approved these changes Feb 5, 2018
Copy link
Contributor

@eliasjpr eliasjpr left a comment

Choose a reason for hiding this comment

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

I am fine with the private method

@marksiemers
Copy link
Contributor Author

@elorest @drujensen

elorest
elorest previously requested changes Feb 9, 2018
Copy link
Member

@elorest elorest left a comment

Choose a reason for hiding this comment

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

Have a couple comments but then lets merge this.

@@ -12,13 +12,7 @@ module Amber::Controller
end

def internal_server_error
response_format("ERROR: #{@ex.inspect_with_backtrace}")
Copy link
Member

Choose a reason for hiding this comment

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

I think we should leave these comments here or else someone will refactor it again because they won't understand why there's added complexity here.

@ex.inspect_with_backtrace
rescue ex : IndexError
@ex.message
rescue
Copy link
Member

Choose a reason for hiding this comment

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

Rather than capturing wild card error and printing "An unknown error occured." we should capture wild card and then print both @ex.message and new ex.mesage. Unknown error message is confusing and hides the fact that there is an actual error.

@@ -33,6 +27,14 @@ module Amber::Controller
end
end

private def internal_server_error_message
Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with moving this into a private method especially since this only gets called if there's an error.

Personally I think we abstract code into methods too much though. In a lot of cases it's just as readable either way and slows down the code minutely every branch we add to the AST.

@marksiemers
Copy link
Contributor Author

Updates made.

We should revisit this code though. It needs to do different things based on environment. Right now, in production, it will return the stack trace or exception messages to the browser. It should do something more like this:

  • In development, do what it is currently doing
  • In production, log the stack trace or exception messages with logger.error (or whatever is appropriate), and return a more user-friendly error message to the browser.

@marksiemers marksiemers dismissed stale reviews from eliasjpr and elorest February 18, 2018 16:44

stale

@eliasjpr eliasjpr merged commit d67e8b5 into master Mar 2, 2018
@eliasjpr eliasjpr deleted the ms/fix-internal_server_error-error branch March 2, 2018 18:48
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.

5 participants