Server error handling

Hello,

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__,
                server=server,
                )

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


@app.errorhandler(HTTPException)
def page_not_found(e):
    return render_template('403.html')

I’d really appreciate pointers if anyone has ideas.

Thanks,

Daniel

1 Like

Hey Daniel,

Here’s what I’m using:

from werkzeug.exceptions import HTTPException, InternalServerError

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

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

Hey @cufflink,

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

And from the run in python is:

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

Daniel

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

from flask import request, render_template

...

@app.server.before_request
def before_request_func():
    try:
        username = request.authorization['username']
    except:
        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.

Daniel

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.server.errorhandler(SomeException)
@app.callback(Output('error-modal', 'is_open),
                           Input(???))
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>
<html>
    <head>
        <title>Error {{name}}</title>
    </head>
    <body>
        <h1>{{name}}</h1>
        <p>{{description}}</p>
        <p>{{code}}</p>
    </body>
</html>

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

Greeting,
Rachel Gomez