forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reformatted code to use spaces instead of tabs
- Loading branch information
1 parent
67db0bc
commit 254861b
Showing
21 changed files
with
347 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .sanic import Sanic | ||
from .sanic import Sanic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
from .response import text | ||
from traceback import format_exc | ||
|
||
|
||
class SanicException(Exception): | ||
def __init__(self, message, status_code=None): | ||
super().__init__(message) | ||
if status_code is not None: | ||
self.status_code = status_code | ||
def __init__(self, message, status_code=None): | ||
super().__init__(message) | ||
if status_code is not None: | ||
self.status_code = status_code | ||
|
||
|
||
class NotFound(SanicException): | ||
status_code = 404 | ||
status_code = 404 | ||
|
||
|
||
class InvalidUsage(SanicException): | ||
status_code = 400 | ||
status_code = 400 | ||
|
||
|
||
class ServerError(SanicException): | ||
status_code = 500 | ||
status_code = 500 | ||
|
||
|
||
class Handler: | ||
handlers = None | ||
def __init__(self, sanic): | ||
self.handlers = {} | ||
self.sanic = sanic | ||
|
||
def add(self, exception, handler): | ||
self.handlers[exception] = handler | ||
|
||
def response(self, request, exception): | ||
""" | ||
Fetches and executes an exception handler and returns a reponse object | ||
:param request: Request | ||
:param exception: Exception to handle | ||
:return: Response object | ||
""" | ||
handler = self.handlers.get(type(exception), self.default) | ||
response = handler(request=request, exception=exception) | ||
return response | ||
|
||
def default(self, request, exception): | ||
if issubclass(type(exception), SanicException): | ||
return text("Error: {}".format(exception), status=getattr(exception, 'status_code', 500)) | ||
elif self.sanic.debug: | ||
return text("Error: {}\nException: {}".format(exception, format_exc()), status=500) | ||
else: | ||
return text("An error occurred while generating the request", status=500) | ||
handlers = None | ||
|
||
def __init__(self, sanic): | ||
self.handlers = {} | ||
self.sanic = sanic | ||
|
||
def add(self, exception, handler): | ||
self.handlers[exception] = handler | ||
|
||
def response(self, request, exception): | ||
""" | ||
Fetches and executes an exception handler and returns a reponse object | ||
:param request: Request | ||
:param exception: Exception to handle | ||
:return: Response object | ||
""" | ||
handler = self.handlers.get(type(exception), self.default) | ||
response = handler(request=request, exception=exception) | ||
return response | ||
|
||
def default(self, request, exception): | ||
if issubclass(type(exception), SanicException): | ||
return text("Error: {}".format(exception), status=getattr(exception, 'status_code', 500)) | ||
elif self.sanic.debug: | ||
return text("Error: {}\nException: {}".format(exception, format_exc()), status=500) | ||
else: | ||
return text("An error occurred while generating the request", status=500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import logging | ||
|
||
logging.basicConfig(level=logging.INFO, format="%(asctime)s: %(levelname)s: %(message)s") | ||
log = logging.getLogger(__name__) | ||
log = logging.getLogger(__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class Middleware: | ||
def __init__(self, process_request=None, process_response=None): | ||
self.process_request = process_request | ||
self.process_response = process_response | ||
def __init__(self, process_request=None, process_response=None): | ||
self.process_request = process_request | ||
self.process_response = process_response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.