0

Also one can also say there is no way to pass custom view function to change or reset password so that any exceptions occurred can be handled.

I my case I have set SECURITY_SEND_PASSWORD_CHANGE_EMAIL and SECURITY_SEND_PASSWORD_RESET_EMAIL to True. However if SMTP is not configured then changing or resetting password fails very badly in debug mode. (see below exception) And in case of production mode no response is sent to http request.

Instead I want to handle such exceptions and show proper error message to user (like SMTP not configured)

File "/lib/python3.4/site-packages/flask_security/utils.py", line 341, in send_mail
    mail.send(msg)
  File "/lib/python3.4/site-packages/flask_mail.py", line 491, in send
    with self.connect() as connection:
  File "/lib/python3.4/site-packages/flask_mail.py", line 144, in __enter__
    self.host = self.configure_host()
  File "/lib/python3.4/site-packages/flask_mail.py", line 158, in configure_host
    host = smtplib.SMTP(self.mail.server, self.mail.port)
  File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python3.4/smtplib.py", line 321, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
    self.source_address)
  File "/usr/lib/python3.4/socket.py", line 512, in create_connection
    raise err
  File "/usr/lib/python3.4/socket.py", line 503, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
Harshal Dhumal
  • 1,259
  • 1
  • 10
  • 18
  • Have you tried to implement a handler for this exception? http://flask.pocoo.org/docs/0.12/patterns/errorpages/ – leovp Nov 29 '17 at 09:42
  • @leovp Those are general error handlers. So if internal server error occurs (http 500) how am I supposed to distinguish it from other internal server errors which may come from other parts of application. As I want to re-render password change form again with proper error message like SMTP not configured or other error message like unable to connect to SMTP sever or SMTP refused connection because of wrong password (SMTP password) supplied and so on. – Harshal Dhumal Nov 29 '17 at 10:47
  • You can handle an arbitrary exception there, or a general Exception if you wish. It's not 100% best, but it's better than a standard "error" page. See: http://flask.pocoo.org/docs/0.12/api/#flask.Flask.errorhandler In any case, you would see exactly what caused this error in Debug mode, but in production I wouldn't leak such details to an end-user. – leovp Nov 29 '17 at 14:49

0 Answers0