Skip to content
This repository was archived by the owner on Sep 3, 2023. It is now read-only.

Commit 3416aa4

Browse files
committed
Closes #2
1 parent 2944f09 commit 3416aa4

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import uvicorn
44
import json
55
from fastapi import FastAPI, Request, Response, status, Form, HTTPException
6+
from starlette.exceptions import HTTPException as StarletteHTTPException
67
from fastapi.responses import HTMLResponse, RedirectResponse
78
from fastapi.staticfiles import StaticFiles
89
from fastapi.templating import Jinja2Templates
@@ -327,5 +328,25 @@ def post_api_report(response: Response,
327328
return RedirectResponse(f"{baseURL}?alert=success", status_code=status.HTTP_303_SEE_OTHER)
328329

329330

331+
"""
332+
----------------------------------------------------------
333+
RUNNER & ERROR HANDLER
334+
----------------------------------------------------------
335+
"""
336+
337+
@app.exception_handler(StarletteHTTPException)
338+
async def my_custom_exception_handler(request: Request, exc: StarletteHTTPException):
339+
"""
340+
Handles exceptions and redirects to correct error page.
341+
"""
342+
343+
if exc.status_code == 404:
344+
return templates.TemplateResponse("error.html", {"request": request, "code": "404", "description": "The requested resource couldn't be found."})
345+
elif exc.status_code == 500:
346+
return templates.TemplateResponse("error.html", {"request": request, "code": "500", "description": exc.detail})
347+
else:
348+
return templates.TemplateResponse('error.html', {"request": request, "code": "Error", "description": exc.detail})
349+
350+
330351
if __name__ == "__main__":
331352
uvicorn.run(app, host="localhost", port=8000)

templates/error.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
7+
<title>Error</title>
8+
<link rel="icon" type="image/png" sizes="300x300" href="/assets/img/ZoI2D6yH_400x400.png">
9+
<link rel="icon" type="image/png" sizes="300x300" href="/assets/img/ZoI2D6yH_400x400.png">
10+
<link rel="icon" type="image/png" sizes="300x300" href="/assets/img/ZoI2D6yH_400x400.png">
11+
<link rel="icon" type="image/png" sizes="300x300" href="/assets/img/ZoI2D6yH_400x400.png">
12+
<link rel="icon" type="image/png" sizes="300x300" href="/assets/img/ZoI2D6yH_400x400.png">
13+
<link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css">
14+
<link rel="stylesheet" href="/assets/css/IBM%20Plex%20Mono.css">
15+
<link rel="stylesheet" href="/assets/css/Space%20Mono.css">
16+
<link rel="stylesheet" href="/assets/css/Hero-Clean.css">
17+
<link rel="stylesheet" href="/assets/css/styles.css">
18+
</head>
19+
20+
<body style="padding: 20px;">
21+
<section class="highlight-clean" style="padding-left: 15px;padding-right: 15px;">
22+
<div class="container">
23+
<div class="intro" style="margin-top: 100px; height: 50vh; margin-top: 30vh;">
24+
<h3 class="text-center" style="font-size: 40px;">{{ code }}</h3>
25+
<p class="text-center" style="color: rgb(179,179,179);">{{ description }}</p>
26+
</div>
27+
</div>
28+
</section>
29+
<script src="/assets/js/jquery.min.js"></script>
30+
<script src="/assets/bootstrap/js/bootstrap.min.js"></script>
31+
</body>
32+
33+
</html>

0 commit comments

Comments
 (0)