Skip to content

Commit

Permalink
Move errors to exceptions to simplify control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjosack committed Jan 7, 2015
1 parent 8a9747a commit 3c559a3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 102 deletions.
3 changes: 2 additions & 1 deletion nsot/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools

from . import exc
from .permissions import PermissionsFlag


Expand Down Expand Up @@ -28,7 +29,7 @@ def wrapper(self, *args, **kwargs):
"without site_id keyword argument."
)
if not _has_any_perm(self.current_user, kwargs["site_id"], perms):
return self.forbidden("Lacking appropriate permissions.")
raise exc.Forbidden("Lacking appropriate permissions.")
return method(self, *args, **kwargs)
return wrapper
return dec
14 changes: 14 additions & 0 deletions nsot/exc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from tornado.web import HTTPError

class Error(Exception):
""" Baseclass for NSoT Exceptions."""

Expand All @@ -6,3 +8,15 @@ class ModelError(Error):

class ValidationError(ModelError):
""" Raised when validation fails on a model."""

class BaseHttpError(HTTPError):
def __init__(self, log_message, *args, **kwargs):
HTTPError.__init__(
self, self.status_code, log_message, *args, **kwargs
)

class BadRequest(BaseHttpError): status_code = 400
class Unauthorized(BaseHttpError): status_code = 401
class Forbidden(BaseHttpError): status_code = 403
class NotFound(BaseHttpError): status_code = 404
class Conflict(BaseHttpError): status_code = 409

0 comments on commit 3c559a3

Please sign in to comment.