Browse Source

[WEBUI] Add error support using exceptions in controller

Yann Weber 8 years ago
parent
commit
ee1d1edd44

+ 51
- 0
plugins/webui/exceptions.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+from werkzeug.wrappers import Response
4
+
5
+class HttpException(Exception):
6
+
7
+    STATUS_STR = {
8
+        4:{
9
+            400: 'Bad request',
10
+            401: 'Unauthorized',
11
+            402: 'Payment required',
12
+            403: 'Forbidden',
13
+            404: 'Not found',
14
+            418: 'I\'m a teapot', #RFC 2324
15
+        },
16
+        5:{
17
+            500: 'Internal server error',
18
+            501: 'Not implemented',
19
+        },
20
+    }
21
+
22
+    def __init__(self, status_code = 500, tpl = 'error.html', custom = None):
23
+        self.status_code = status_code
24
+        self.tpl = tpl
25
+        self.custom = custom
26
+
27
+    def render(self, request):
28
+        from .interface.template.loader import TemplateLoader
29
+        loader = TemplateLoader()
30
+        tpl_vars = {
31
+            'status_code': self.status_code,
32
+            'status_str': self.status_str(self.status_code),
33
+            'custom': self.custom }
34
+        response = Response(
35
+            loader.render_to_response(self.tpl, template_vars = tpl_vars),
36
+            mimetype = 'text/html')
37
+        response.status_code = self.status_code
38
+        return response
39
+
40
+    @staticmethod
41
+    def status_str(status_code):
42
+        status_fam = status_code / 100
43
+        if status_fam not in HttpException.STATUS_STR or \
44
+            status_code not in HttpException.STATUS_STR[status_fam]:
45
+            return 'Unknown'
46
+        else:
47
+            return HttpException.STATUS_STR[status_fam][status_code]
48
+
49
+
50
+
51
+        

+ 1
- 3
plugins/webui/interface/controllers/base.py View File

5
 
5
 
6
 # This module contains the web UI controllers that will be called from the web ui class
6
 # This module contains the web UI controllers that will be called from the web ui class
7
 
7
 
8
-
9
-def get_response(tpl, tpl_vars={}, mimetype='text/html', status_code=200):
8
+def get_response(tpl='empty.html', tpl_vars={}, mimetype='text/html', status_code=200):
10
     loader = TemplateLoader()
9
     loader = TemplateLoader()
11
     response = Response(loader.render_to_response(tpl, template_vars=tpl_vars), mimetype=mimetype)
10
     response = Response(loader.render_to_response(tpl, template_vars=tpl_vars), mimetype=mimetype)
12
     response.status_code = status_code
11
     response.status_code = status_code
13
     return response
12
     return response
14
 
13
 
15
-
16
 def index(request):
14
 def index(request):
17
     return get_response('index/index.html')
15
     return get_response('index/index.html')
18
 
16
 

+ 15
- 3
plugins/webui/run.py View File

3
 
3
 
4
 import os
4
 import os
5
 from werkzeug.contrib.sessions import FilesystemSessionStore
5
 from werkzeug.contrib.sessions import FilesystemSessionStore
6
+from werkzeug.wrappers import Response
6
 
7
 
7
 from lodel.settings import Settings
8
 from lodel.settings import Settings
8
 from .interface.router import get_controller
9
 from .interface.router import get_controller
9
 from .interface.lodelrequest import LodelRequest
10
 from .interface.lodelrequest import LodelRequest
11
+from .exceptions import *
10
 from lodel.utils.datetime import get_utc_timestamp
12
 from lodel.utils.datetime import get_utc_timestamp
11
 
13
 
12
 SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
14
 SESSION_FILES_BASE_DIR = Settings.webui.sessions.directory
55
             request.session = session_store.new()
57
             request.session = session_store.new()
56
             request.session['user_context'] = None
58
             request.session['user_context'] = None
57
         request.session['last_accessed'] = current_timestamp
59
         request.session['last_accessed'] = current_timestamp
58
-
59
-    controller = get_controller(request)
60
-    response = controller(request)
60
+    
61
+    try:
62
+        controller = get_controller(request)
63
+        response = controller(request)
64
+    except HttpException as e:
65
+        try:
66
+            response = e.render(request)
67
+        except Exception as eb:
68
+            res = Response()
69
+            res.status_code = 500
70
+            return res
71
+        
72
+        
61
     if request.session.should_save:
73
     if request.session.should_save:
62
         session_store.save(request.session)
74
         session_store.save(request.session)
63
         response.set_cookie('sid', request.session.sid)
75
         response.set_cookie('sid', request.session.sid)

+ 0
- 0
plugins/webui/templates/empty.html View File


+ 9
- 0
plugins/webui/templates/error.html View File

1
+<!DOCTYPE html>
2
+<html>
3
+	<head>
4
+		<title>{{status_code}} {{status_str}}</title>
5
+	</head>
6
+	<body>
7
+		{{status_code}} {{status_str}}
8
+	</body>
9
+</html>

Loading…
Cancel
Save