Skip to content

Commit

Permalink
add error page
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanLuLyf committed Mar 15, 2020
1 parent 139bad7 commit 1f1aec3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include asset/*.html
61 changes: 61 additions & 0 deletions asset/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>BunnyPy Error</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
<style>
* {
margin: 0;
padding: 0;
}

.title {
border-bottom: 1px solid #9baeca;
color: #9baeca;
padding: 0.75rem;
}

.sub-title {
margin: 0.75rem;
}

.link {
color: deepskyblue;
text-decoration: none;
}

.message {
margin: 0.75rem;
padding: 0.75rem;
border-radius: 0.25rem;
background: mistyrose;
}

.message table {
border-collapse: collapse;
width: 100%;
}

.message table td, .message table th {
border: 1px solid lightyellow;
color: #666;
height: 30px;
}

.message table tr:nth-child(odd) {
background: white;
}

.message table tr:nth-child(even) {
background: floralwhite;
}
</style>
</head>
<body>
<h1 class="title">BunnyPy Error</h1>
<pre class="message">{{ bunny_error }}</pre>
<p class="message">Powered By <a class="link" href="http://github.com/IvanLuLyf/BunnyPy">BunnyPy</a></p>
</body>
</html>
22 changes: 15 additions & 7 deletions bunnypy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import os
import re
from pathlib import Path
from html import escape
from urllib.parse import parse_qs, unquote
from wsgiref.simple_server import make_server

__version__ = "0.2.1"
__version__ = "0.2.2"

__default_html__ = '''<html lang="en"><head><meta charset="utf-8"><title>Welcome to BunnyPy</title>
<style>body{width: 35em;margin: 0 auto;text-align: center;}</style></head><body>
Expand All @@ -28,6 +29,8 @@
'''

__asset_dir__ = os.path.dirname(os.path.abspath(__file__)) + '/asset/'


class Bunny:
__sub_apps__ = {}
Expand All @@ -41,7 +44,7 @@ def handler(self, environ, start_response):
with file_path.open("rb") as static_file:
data = static_file.read()
except FileNotFoundError:
data = b'<h1>404 Not Found</h1>'
data = self.__render_error('404 Not Found');
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
return [data]
url_array = environ['PATH_INFO'].split('/')
Expand Down Expand Up @@ -97,9 +100,9 @@ def __call_action__(self, req, name, action):
if ac_other is not None:
return self.__call_func__(ac_other, req)
else:
return 'Action {0} Not Exists'.format(action)
return self.__render_error('Action {0} Not Exists'.format(action))
else:
return 'Mod {0} Not Exists'.format(name)
return self.__render_error('Mod {0} Not Exists'.format(name))

def __call_func__(self, func, req):
try:
Expand All @@ -117,11 +120,14 @@ def __call_func__(self, func, req):
args.append(req[vn])
return func(*args)
except Exception as e:
return '<h1>BunnyPy Error</h1><p>{0}</p>'.format(str(e))
return self.__render_error(str(e))

def render(self, view, context=None):
return self.TemplateRender(view, context).render()

def __render_error(self, error_str):
return self.TemplateRender('error.html', {'bunny_error': error_str}, __asset_dir__).render()

class Request:
def __init__(self, environ):
query_string = environ['QUERY_STRING']
Expand Down Expand Up @@ -401,9 +407,11 @@ def __str__(self):
return '\n'.join(map(str, self.lines))

class TemplateRender:
def __init__(self, view, context=None):
def __init__(self, view, context=None, base_path=None):
if base_path is None:
base_path = "template/"
try:
with open("template/" + view, "r", encoding='utf-8') as template_file:
with open(base_path + view, "r", encoding='utf-8') as template_file:
self.raw_text = template_file.read()
except FileNotFoundError:
self.raw_text = '<h1>Template' + view + ' Not Found</h1>'
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
url='https://github.com/ivanlulyf/bunnypy',
py_modules=['bunnypy'],
scripts=['bunnypy.py'],
include_package_data=True,
license='MIT',
platforms='any',
classifiers=['Operating System :: OS Independent',
Expand Down

0 comments on commit 1f1aec3

Please sign in to comment.