Wilson Xu avatar Wilson Xu committed 4cf23ce

Added errors handlers.

Comments (0)

Files changed (5)

 
 import os
 
-from flask import Flask, request
+from flask import Flask, request, render_template
 from flaskext.babel import Babel
 
 from fbone import utils
     configure_logging(app)
     configure_template_filters(app)
     configure_i18n(app)
-    
+    configure_error_handlers(app)
+
     return app
 
 
     db.init_app(app)
     mail.init_app(app)
     cache.init_app(app)
-    
+
 
 def configure_login_manager(app):
     login_manager.login_view = 'frontend.login'
 
 def configure_logging(app):
     """Add log file handler and email handler."""
-    
+
     if app.debug or app.testing:
         return
 
     import logging
     from logging.handlers import RotatingFileHandler, SMTPHandler
-    
+
     # Set info level on logger, which might be overwritten by handers.
     app.logger.setLevel(logging.INFO)
 
         '[in %(pathname)s:%(lineno)d]')
     )
     app.logger.addHandler(file_handler)
-    
+
     ADMINS = ['imwilsonxu@gmail.com']
     mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                app.config['MAIL_USERNAME'],
     @app.before_request
     def before_request():
         pass
+
+
+def configure_error_handlers(app):
+
+    @app.errorhandler(403)
+    def forbidden_page(error):
+        return render_template("errors/forbidden_page.html"), 403
+
+    @app.errorhandler(404)
+    def page_not_found(error):
+        return render_template("errors/page_not_found.html"), 404
+
+    @app.errorhandler(405)
+    def method_not_allowed_page(error):
+        return render_template("errors/method_not_allowed.html"), 405
+
+    @app.errorhandler(500)
+    def server_error_page(error):
+        return render_template("errors/server_error.html"), 500

fbone/templates/errors/forbidden_page.html

+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Page Not Found :(</title>
+  <style>
+    body { text-align: center;}
+    h1 { font-size: 50px; text-align: center }
+    span[data-frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
+    body { font: 20px Constantia, 'Hoefler Text',  "Adobe Caslon Pro", Baskerville, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 200, 0.5); }
+    ::-moz-selection{ background:#FF5E99; color:#fff; }
+    ::selection { background:#FF5E99; color:#fff; }
+    article {display:block; text-align: left; width: 500px; margin: 0 auto; }
+
+    a { color: rgb(36, 109, 56); text-decoration:none; }
+    a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56, 0.5); }
+  </style>
+</head>
+<body>
+  <article>
+    <h1>Forbidden page <span data-frown>:(</span></h1>
+  </article>
+</body>
+</html>

fbone/templates/errors/method_not_allowed.html

+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Page Not Found :(</title>
+  <style>
+    body { text-align: center;}
+    h1 { font-size: 50px; text-align: center }
+    span[data-frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
+    body { font: 20px Constantia, 'Hoefler Text',  "Adobe Caslon Pro", Baskerville, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 200, 0.5); }
+    ::-moz-selection{ background:#FF5E99; color:#fff; }
+    ::selection { background:#FF5E99; color:#fff; }
+    article {display:block; text-align: left; width: 500px; margin: 0 auto; }
+
+    a { color: rgb(36, 109, 56); text-decoration:none; }
+    a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56, 0.5); }
+  </style>
+</head>
+<body>
+  <article>
+    <h1>Not allowed <span data-frown>:(</span></h1>
+  </article>
+</body>
+</html>

fbone/templates/errors/page_not_found.html

+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Page Not Found :(</title>
+  <style>
+    body { text-align: center;}
+    h1 { font-size: 50px; text-align: center }
+    span[data-frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
+    body { font: 20px Constantia, 'Hoefler Text',  "Adobe Caslon Pro", Baskerville, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 200, 0.5); }
+    ::-moz-selection{ background:#FF5E99; color:#fff; }
+    ::selection { background:#FF5E99; color:#fff; }
+    article {display:block; text-align: left; width: 500px; margin: 0 auto; }
+
+    a { color: rgb(36, 109, 56); text-decoration:none; }
+    a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56, 0.5); }
+  </style>
+</head>
+<body>
+  <article>
+    <h1>Not found <span data-frown>:(</span></h1>
+    <div>
+      <p>Sorry, but the page you were trying to view does not exist.</p>
+      <p>It looks like this was the result of either:</p>
+      <ul>
+        <li>a mistyped address</li>
+        <li>an out-of-date link</li>
+      </ul>
+    </div>
+
+    <script>
+      var GOOG_FIXURL_LANG = (navigator.language || '').slice(0,2),
+      GOOG_FIXURL_SITE = location.host;
+    </script>
+    <script src="http://linkhelp.clients.google.com/tbproxy/lh/wm/fixurl.js"></script>
+  </article>
+</body>
+</html>

fbone/templates/errors/server_error.html

+<!doctype html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Page Not Found :(</title>
+  <style>
+    body { text-align: center;}
+    h1 { font-size: 50px; text-align: center }
+    span[data-frown] { transform: rotate(90deg); display:inline-block; color: #bbb; }
+    body { font: 20px Constantia, 'Hoefler Text',  "Adobe Caslon Pro", Baskerville, Georgia, Times, serif; color: #999; text-shadow: 2px 2px 2px rgba(200, 200, 200, 0.5); }
+    ::-moz-selection{ background:#FF5E99; color:#fff; }
+    ::selection { background:#FF5E99; color:#fff; }
+    article {display:block; text-align: left; width: 500px; margin: 0 auto; }
+
+    a { color: rgb(36, 109, 56); text-decoration:none; }
+    a:hover { color: rgb(96, 73, 141) ; text-shadow: 2px 2px 2px rgba(36, 109, 56, 0.5); }
+  </style>
+</head>
+<body>
+  <article>
+    <h1>Server error <span data-frown>:(</span></h1>
+  </article>
+</body>
+</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.