Server error handling


I have been using BasicAuth to manage the logins in my app, but when the user goes to any other page than the main, they get a 403 error. Has anyone successfully implemented serer.errorhandler with Dash?

Here’s what I tried:

server = Flask(__name__)
bootstrap = Bootstrap(server)

app = dash.Dash(__name__,

def no_access(e):
    return render_template('403.html'), 403

server.register_error_handler(403, no_access)

But that didn’t work and neither did:

from werkzeug.exceptions import HTTPException

def page_not_found(e):
    return render_template('403.html')

I’d really appreciate pointers if anyone has ideas.



1 Like

Hey Daniel,

Here’s what I’m using:

from werkzeug.exceptions import HTTPException, InternalServerError

# This generates a dynamic HTTP exception page
def handle_http_exception(e):
    return render_template("http_exception.html", code=e.code,, description=e.description)

# This responds with an internal server error page
def handle_internal_server_error(e):
    return render_template("http_exception.html", code=e.code,, description=e.description)

Hey @cufflink,

Thanks for the suggestion, but it didn’t work for me:

And from the run in python is:

I don’t understand why it’s just not pulling the server.errorhandler.


I reproduced your issue. Kind of hacky, but this worked for me.

from flask import request, render_template


def before_request_func():
        username = request.authorization['username']
        username = None
    base_url = request.base_url
    host_url = request.host_url

    if username is None and base_url != host_url:
        return render_template("403.html")

That renders all pages outside of the host_url with a 403 page unless authorized.

Hey @cufflink,

That really did the trick! Thanks, it worked like a charm.


Is it possible to use @app.server.errorhandler as Input or caller for a callback?
I’m trying to e.g. open a modal with an error message when an exception occurrs.
so something like:

@app.callback(Output('error-modal', 'is_open),
def _open_error_modal(???):
   return True

Is this really working for you inside a flask/dash app?
I thought this would be impossible: Dash with Flask - #2 by nedned
When I try to do the same with an almost empty html page in templates/, only containing:

<!doctype html>
        <title>Error {{name}}</title>

I get Unexpected token < in JSON at position 0
I do not quite understand where to the rendered template gets send. Clearly, dash/flask is expecting json and not html as the error message states. (I also tried to jsonify the rendered page which obviously didn’t work).

However what I’m ultimately still trying to do is to change the content of any div within my dash/flask app when handle_internal_server_error() is called but this seems to be impossible as callbacks can only be called from components of the app and not programmatically.

It means that the server encountered an unexpected condition that prevented it from fulfilling the request. This error is usually returned by the server when no other error code is suitable

Rachel Gomez