Skip to content

Commit

Permalink
Merge pull request #34 from alex-oleshkevich/exception-notes
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich committed Jun 21, 2023
2 parents 851d312 + 72942cc commit adcdff7
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 524 deletions.
8 changes: 8 additions & 0 deletions examples/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ def css_view(request: Request) -> Response:
return templates.TemplateResponse('csstest.css', {'request': request})


def exception_notes_view(request: Request) -> typing.NoReturn:
exc = ValueError('This is the first cause')
if hasattr(exc, 'add_note'): # py311
exc.add_note('Some more info')
raise exc


install_error_handler(editor='vscode')
app = Starlette(
debug=True,
routes=[
Route('/', index_view),
Route('/hint', hint_view),
Route('/chain', chain_view),
Route('/notes', exception_notes_view),
Route('/css', css_view),
Route('/template', template_view),
Route('/javascript', javascript_view),
Expand Down
964 changes: 447 additions & 517 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pytest = "^7.2"
pytest-asyncio = "^0.18.0"
pytest-cov = "^4.0"
black = "^22.6.0"
mypy = "^v0.971"
mypy = "^1.4"
flake8 = "^4.0.1"
uvicorn = "^0.18.2"
fastapi = "^0.79.0"
Expand Down
4 changes: 4 additions & 0 deletions starception/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class StackItem:
frames: typing.List[inspect.FrameInfo]
has_vendor_frames: bool
solution: str
notes: typing.List[str]


def generate_html(request: Request, exc: Exception, limit: int = 15) -> str:
Expand All @@ -237,6 +238,7 @@ def generate_html(request: Request, exc: Exception, limit: int = 15) -> str:
StackItem(
exc=exc,
solution=getattr(exc, 'solution', ''),
notes=getattr(exc, '__notes__', []),
frames=frames,
has_vendor_frames=any(is_vendor(f) for f in frames),
)
Expand All @@ -249,6 +251,7 @@ def generate_html(request: Request, exc: Exception, limit: int = 15) -> str:
StackItem(
exc=cause,
solution=getattr(cause, 'solution', ''),
notes=getattr(exc, '__notes__', []),
frames=frames,
has_vendor_frames=any(is_vendor(f) for f in frames),
)
Expand Down Expand Up @@ -299,6 +302,7 @@ def generate_html(request: Request, exc: Exception, limit: int = 15) -> str:
},
'environment': os.environ,
'solution': getattr(exc, 'solution', None),
'notes': getattr(exc, '__notes__', []),
}
)

Expand Down
26 changes: 22 additions & 4 deletions starception/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@
</button>
</div>
</div>
{% if solution %}
<div class="solution">Hint: {{ solution }}</div>
{% if solution or notes %}
<div class="solution">
<ul>
{% if solution %}
<li>{{ solution }}</li>
{% endif %}
{% for note in notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
</header>
<main>
Expand All @@ -56,9 +65,18 @@
{% endif %}
<div data-trace-target {% if loop.index0 > 0 %} class="collapsed"{% endif %}>
{% if loop.index0 > 0 %}
{% if stack_item.solution %}
{% if stack_item.solution or stack_item.notes %}
<div style="padding: 0 48px">
<div class="solution" style="margin-top: 8px">Hint: {{ stack_item.solution }}</div>
<div class="solution" style="margin-top: 8px">
<ul>
{% if stack_item.solution %}
<li>{{ stack_item.solution }}</li>
{% endif %}
{% for note in stack_item.notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
{% endif %}
Expand Down
6 changes: 4 additions & 2 deletions starception/templates/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ function restoreColorTheme() {
*/
function bindStackBlock(el) {
restoreColorTheme();
bindThemeToggle();
bindVendorFramesToggle(el);
bindExceptionBlocks(el);
bindCodeSnippets(el);
Expand All @@ -147,4 +146,7 @@ function bindStackBlocks() {
document.querySelectorAll('[data-stack-root]').forEach(bindStackBlock);
}

document.addEventListener('DOMContentLoaded', bindStackBlocks);
document.addEventListener('DOMContentLoaded', () => {
bindThemeToggle();
bindStackBlocks();
});
10 changes: 10 additions & 0 deletions starception/templates/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,13 @@ dd {
.text-error {
color: var(--red);
}

.solution ul {
margin: 0;
padding: 0;
}

.solution ul li {
margin: 0 0 0 20px;
padding: 0;
}

0 comments on commit adcdff7

Please sign in to comment.