-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf7c1c3
commit 4d61bc9
Showing
14 changed files
with
264 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from blacksheep.messages import Request | ||
|
||
|
||
def get_request_url(request: Request) -> str: | ||
protocol = request.scope.get("type") | ||
host, port = request.scope.get("server") | ||
|
||
if protocol == "http" and port == 80: | ||
port_part = "" | ||
elif protocol == "https" and port == 443: | ||
port_part = "" | ||
else: | ||
port_part = f":{port}" | ||
|
||
return f"{protocol}://{host}{port_part}{request.url.value.decode()}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import html | ||
import traceback | ||
|
||
from blacksheep.contents import HtmlContent | ||
from blacksheep.messages import Request, Response | ||
from blacksheep.server.asgi import get_request_url | ||
from blacksheep.server.resources import get_resource_file_content | ||
|
||
|
||
def _load_error_page_template() -> str: | ||
error_css = get_resource_file_content("error.css") | ||
error_template = get_resource_file_content("error.html") | ||
assert "/*STYLES*/" in error_template | ||
|
||
# since later it is used in format_map... | ||
error_css = error_css.replace("{", "{{").replace("}", "}}") | ||
return error_template.replace("/*STYLES*/", error_css) | ||
|
||
|
||
class ServerErrorDetailsHandler: | ||
""" | ||
This class is responsible of producing a detailed response when the Application is | ||
configured to show error details to the client, and an unhandled exception happens. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self._error_page_template = _load_error_page_template() | ||
|
||
def produce_response(self, request: Request, exc: Exception) -> Response: | ||
tb = traceback.format_exception(exc.__class__, exc, exc.__traceback__) | ||
info = "" | ||
for item in tb: | ||
info += f"<li><pre>{html.escape(item)}</pre></li>" | ||
|
||
content = HtmlContent( | ||
self._error_page_template.format_map( | ||
{ | ||
"info": info, | ||
"exctype": exc.__class__.__name__, | ||
"excmessage": str(exc), | ||
"method": request.method, | ||
"path": request.url.value.decode(), | ||
"full_url": get_request_url(request), | ||
} | ||
) | ||
) | ||
|
||
return Response(500, content=content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
body { | ||
font-family: sans-serif; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
dl { | ||
display: flex; | ||
flex-flow: row; | ||
flex-wrap: wrap; | ||
overflow: visible; | ||
} | ||
|
||
dt { | ||
font-weight: bold; | ||
flex: 0 0 20%; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
border-bottom: 1px dotted #eee; | ||
padding: 10px 0; | ||
} | ||
|
||
dd { | ||
flex: 0 0 80%; | ||
margin-left: auto; | ||
text-align: left; | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
padding: 10px 0; | ||
border-bottom: 1px dotted #eee; | ||
} | ||
|
||
dt, dd { | ||
padding: .3rem 0; | ||
} | ||
|
||
header { | ||
background-color: #1c6962; | ||
color: white; | ||
padding: .5rem 2rem; | ||
border-bottom: 1px solid #0f5046; | ||
} | ||
|
||
#content { | ||
padding: .5rem 2rem; | ||
} | ||
|
||
.stack-trace { | ||
font-family: monospace; | ||
background: aliceblue; | ||
padding: 1rem 3.5rem; | ||
border: 1px dashed #366a62; | ||
font-size: 14px; | ||
} | ||
|
||
.stack-trace pre { | ||
padding-left: 60px; | ||
word-break: break-word; | ||
white-space: break-spaces; | ||
} | ||
|
||
.custom-counter { | ||
margin: 0; | ||
padding: 0 1rem 0 0; | ||
list-style-type: none; | ||
} | ||
|
||
.custom-counter li { | ||
counter-increment: step-counter; | ||
margin-bottom: 5px; | ||
} | ||
|
||
.custom-counter li::before { | ||
content: counter(step-counter); | ||
margin-right: 20px; | ||
font-size: 80%; | ||
background-color: rgb(54 106 98); | ||
color: white; | ||
font-weight: bold; | ||
padding: 3px 8px; | ||
float: left; | ||
border-radius: 11px; | ||
margin-left: 20px; | ||
} | ||
|
||
.notes { | ||
padding: 1rem 0; | ||
font-size: small; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.