Output exception to browser when Flask app has error - python

When my deployed Flask application has an error, I only see a standard error message in the browser.
Internal Server Error
The server encountered an internal error and was unable to complete
your request. Either the server is overloaded or there is an error in
the application.
Going through the logs to find the error is inconvenient. How can I output the error to the browser instead?

For debugging purposes you may try this:
import traceback
#app.errorhandler(Exception)
def handle_500(e=None):
app.logger.error(traceback.format_exc())
return 'Internal server error occured', 500

Related

HTTPError: HTTP Error 403: Forbidden. wget with python

I am using Python with Jupyter Notebook.
This program downloads many pdfs and works on other machines, however when putting it on another machine with Windows Server 2016 Standard it shows an error.
The function that is causing the error is:
def download_doc(pasta_pdf,base_links):
os.chdir(pasta_pdf)
for link in base_links['Link_Download_Regulamento']:
if link != None:
wget.download(link)
else:
continue
download_doc(pasta_origem,df_regulamentos_novos)
The error print:
enter image description here
Thanks for your assistance.
You got
HTTPError: HTTP Error 403: Forbidden
If you got information about HTTP response status code, but do not know what it does mean, then you might consult developer.mozilla.org docs, in this case this is 403 Forbidden
The client does not have access rights to the content; that is, it is
unauthorized, so the server is refusing to give the requested
resource. Unlike 401, the client's identity is known to the server.

Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed

I am trying to create simple python eve app with following code:
But i am getting the following error in postman:
Could not get any response
There was an error connecting to http://127.0.0.1:27017/people.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
Self-signed SSL certificates are being blocked:
Fix this by turning off 'SSL certificate verification' in Settings > General
Proxy configured incorrectlyenter code here
Ensure that proxy is configured correctly in Settings > Proxy
Request timeout:
Change request timeout in Settings > General
and in the mongodb server, got the error as :
2018-08-11T00:36:36.565+0530 I NETWORK [conn3] Error receiving request from client: SSLHandshakeFailed: SSLHandshakeFailed. Ending connection from 127.0.0.1:60241 (connection id: 3)
Is there i am missing anything?
Can you please help in resolving this issue?
Regards,
Narendra

Getting warning and error at same time in tornado proxy

It seems like i am hitting some race condition in between the time I stop my server and during that I make request through tornado proxy frontend.
I get very famous error
WARNING:tornado.access:404 POST /request-url/eff74/36eb5e9f-def1-4689-ad58-3bf866798864/client-update (::1) 0.88ms
ERROR:tornado.general:Cannot send error response after headers written
WARNING:tornado.access:404 POST /request-url/eff74/36eb5e9f-def1-4689-ad58-3bf866798864/client-update (::1) 1.36ms
ERROR:tornado.general:Cannot send error response after headers written
WARNING:tornado.access:404 POST /request-url/eff74/36eb5e9f-def1-4689-
which is described in source code here
Can I resolve this problem?
Point of confusion is I am getting 404 already, then why I am getting an error after wards.
Cheers
Actually, it was very silly bug in my code.
Bug was the proxy function which was handling "requests" wasn't issuing "return" statement from the function.
Example:
def proxy_handler(self, request, response):
# some task
response.set_status(status_code)
response.send("status_code: message")
return # this was missing earlier
So tornado was trying to send respons which already has been sent. Thus getting the error
WARNING:tornado.access:404 POST /request-url/eff74/36eb5e9f-def1-4689-ad58-3bf866798864/client-update (::1) 1.36ms
ERROR:tornado.general:Cannot send error response after headers written
Cheers

in tweepy get_authorization_url() locally HTTP Error 401: Unauthorized but in the web works correctly

I am developing with google appengine sdk and python, I'm trying to incorporate my twiiter app
import tweepy
TWITTER_CONSUMER_KEY = 'XXXXXXXXXXX'
TWITTER_CONSUMER_SECRET = 'xxxxxxxxxx'
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET)
but it does not work, return the following error
in get_authorization_url
raise TweepError(e)
TweepError: HTTP Error 401: Unauthorized
The problem is that this error is generated locally, but the upload my application on the web, works perfectly
I started digging into the Tweepy code and the error returned was this:
Desktop applications only
support the oauth_callback value \'oob\'
I got around it by setting a callback url in my application. I set mine to 127.0.0.1 and that seems to have fixed it.

Flask giving an internal server error instead of rendering 404

In my Flask app, I set up a 404 handler like this:
#app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
However, when a user goes to an unrecognized URL, the system gives an internal server error instead of rendering my 404 template. Am I missing something?
Internal Server Error is HTTP error 500 rather than 404 and you haven't added error handler for it. This occurs when the server is unable to fulfill the client request properly. To add a gracious message when such error occurred, you can add a errorhandler like 404.
#app.errorhandler(500)
def exception_handler(e):
return render_template('500.html'), 500
There is likely an issue while rendering the 404 template which then triggers an internal server error.
I suggest checking the logs for your app.
This will also occur if you have debug set to true. Try setting debug to false and see if your custom 404 shows up then.

Categories