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

Do not show stacktraces on some intentionally-thrown errors #9056

Merged
merged 5 commits into from Feb 5, 2020

Conversation

willbarrett
Copy link
Member

@willbarrett willbarrett commented Jan 31, 2020

CATEGORY

Choose one

  • Bug Fix
  • Enhancement (new features, refinement)
  • Refactor
  • Add tests
  • Build / Development Environment
  • Documentation

SUMMARY

Some endpoints on Superset, notably the connection test endpoint, would display full stacktraces when a connection test failed. We want to display the underlying error message to help with configuration of database connections, but not the full stacktrace. The connection test endpoint was also returning a 500 when a connection did not succeed - a 400 would be more accurate, as the system is attempting to validate user input.

The front-end of this endpoint was looking at the wrong key in the returned dictionary for the error message as well.

Eventually this should be moved under /api/v1, but I'll save that for a different PR.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TEST PLAN

  • Attempt a connection test that fails
  • Verify that the resulting response does not contain a stacktrace

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

REVIEWERS

@villebro @mistercrunch @etr2460

@willbarrett willbarrett marked this pull request as ready for review January 31, 2020 17:37
@codecov-io
Copy link

Codecov Report

Merging #9056 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #9056   +/-   ##
=======================================
  Coverage   59.45%   59.45%           
=======================================
  Files         369      369           
  Lines       11747    11747           
  Branches     2888     2888           
=======================================
  Hits         6984     6984           
  Misses       4584     4584           
  Partials      179      179
Impacted Files Coverage Δ
.../assets/src/SqlLab/components/AceEditorWrapper.jsx 54.21% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2629c77...f72bbb4. Read the comment docs.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

Small comment, other than that LGTM

@@ -1363,7 +1363,9 @@ def testconn(self):
except Exception as e:
logging.exception(e)
return json_error_response(
"Connection failed!\n\n" "The error message returned was:\n{}".format(e)
"Connection failed!\n\n"
"The error message returned was:\n{}".format(e),
Copy link
Member

Choose a reason for hiding this comment

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

While logging is usually done with format to save cycles, in this instance I believe using a regular f-string is the correct course of action.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the review Ville! I'm happy to use either approach. As I'm still somewhat new to Python, it would be very useful for me to understand the reasoning behind your suggestion. Would you mind expanding a little bit?

Copy link
Member

Choose a reason for hiding this comment

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

As Superset moved to Python 3.6+, we now generally prefer to use f-strings over "".format() whenever possible. This is mostly due to f-strings being more readable, but they are usually also slightly more performant in normal usage. While the codebase is still filled with format(),when touching old code the convention has been to change them to f-strings. An exception to this rule is logging, which pylint (I believe) checks for, for which one should generally use logger.debug('message: %s', c) style. Check https://docs.python.org/3/howto/logging.html#optimization

Hope this helps!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that clarifies it. Thank you!

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm...in this case @villebro the line number you honed in on is returning an error response, not a logger call. I think that means "".format() is appropriate following the logic you posted?

Copy link
Member

@villebro villebro Feb 4, 2020

Choose a reason for hiding this comment

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

Sorry @willbarrett , I might have been unclear in my comment. What I meant to say was that in the above case I think using an f-string should be ok, and that f-strings should mainly be avoided in logging messages. The same applies to translations, in which variable text should not be burned into the message prior to translation.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK @villebro I'm confused. What change precisely are you looking for on this PR?

Copy link
Member

Choose a reason for hiding this comment

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

@willbarrett in my original comment I was pointing out when one should and should not use f-strings. In the case above I would personally write

            return json_error_response(
                "Connection failed!\n\n"
                f"The error message returned was:\n{e}",
                400,
            )

Copy link
Member Author

Choose a reason for hiding this comment

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

updated

@villebro villebro merged commit fc1c942 into apache:master Feb 5, 2020
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.36.0 labels Feb 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 0.36.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants