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

error 500 custom template #247

Closed
advenn opened this issue Apr 13, 2022 · 1 comment
Closed

error 500 custom template #247

advenn opened this issue Apr 13, 2022 · 1 comment
Labels
enhancement New feature or request fixed in branch Already fixed in a branch, waiting to be merged needs docs

Comments

@advenn
Copy link

advenn commented Apr 13, 2022

Hi there! Can I use custom html template for Internal Server Error 500? If yes then how to do it?

@RobertoPrevato
Copy link
Member

Hi @advenn
Sorry for not replying yesterday.

Currently the only way to customize responses for otherwise unhandled exceptions is to override a method of the Application class, like in this example:

import textwrap
from datetime import datetime

from blacksheep import Application, html


class MyApplication(Application):
    async def handle_internal_server_error(self, request, exc):
        return html(
            textwrap.dedent(
                """
                <!doctype html>
                <html lang="en">
                <head>
                    <title>Error Page</title>
                    <meta charset="utf-8">
                    <meta name="viewport" content="width=device-width,initial-scale=1">
                    <meta name="description" content="Oh, no!">
                </head>
                <body>
                    <h1>Something went wrong</h1>
                </body>
                </html>
                """.strip(
                    "\n"
                )
            )
        )


app = MyApplication()


@app.route("/")
async def crash_test():
    raise TypeError("Something goes wrong!")

Now that we are speaking about this, it would make sense to make this configurable. On the other hand, the framework provides several ways to handle responses for well-known, thrown exceptions. For example:

import textwrap
from datetime import datetime

from blacksheep import Application, Request, Response, HTTPException, html

app = Application(show_error_details=True)


@app.route("/")
async def home():
    return f"Hello, World! {datetime.utcnow().isoformat()}"


@app.route("/crash")
def crash_test():
    raise HTTPException(500, "Something goes wrong!")


@app.exception_handler(500)
async def internal_server_error_han(
    self, request: Request, exc: HTTPException
) -> Response:
    return html(
        textwrap.dedent(
            """
            <!doctype html>
            <html lang="en">
            <head>
                <title>Error Page</title>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width,initial-scale=1">
                <meta name="description" content="Oh, no!">
            </head>
            <body>
                <h1>Something went wrong</h1>
            </body>
            </html>
            """.strip(
                "\n"
            )
        )
    )

@RobertoPrevato RobertoPrevato added enhancement New feature or request needs docs labels Apr 16, 2022
RobertoPrevato added a commit that referenced this issue Apr 26, 2022
@RobertoPrevato RobertoPrevato added the fixed in branch Already fixed in a branch, waiting to be merged label Apr 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fixed in branch Already fixed in a branch, waiting to be merged needs docs
Projects
None yet
Development

No branches or pull requests

2 participants