-
Notifications
You must be signed in to change notification settings - Fork 81
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
Labels
enhancement
New feature or request
fixed in branch
Already fixed in a branch, waiting to be merged
needs docs
Comments
Hi @advenn Currently the only way to customize responses for otherwise unhandled exceptions is to override a method of the 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
added a commit
that referenced
this issue
Apr 26, 2022
Merged
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
Hi there! Can I use custom html template for
Internal Server Error
500? If yes then how to do it?The text was updated successfully, but these errors were encountered: