Skip to content

Commit

Permalink
Merge pull request #11 from SoftwearDevelopment/kareem/SWPY-1074-chan…
Browse files Browse the repository at this point in the history
…ge-error_view-functionality

SWPY-1074-change-error_view-functionality
  • Loading branch information
Kareeeeem committed Mar 16, 2017
2 parents bebbd3d + 7a458eb commit e95ac2d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
7 changes: 2 additions & 5 deletions spynl/main/error_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pyramid.httpexceptions import (HTTPForbidden, HTTPNotFound,
HTTPInternalServerError)

from spynl.main.exceptions import SpynlException
from spynl.main.utils import log_error
from spynl.main.locale import SpynlTranslationString as _

Expand All @@ -26,9 +25,7 @@ def spynl_error(exc, request):
top_msg = "Spynl Error of type %s with message: '%s'."
message = exc.message
log_error(exc, request, top_msg, exc.__class__.__name__, message)
return {'status': 'error',
'type': exc.__class__.__name__,
'message': message}
return exc.make_response()


def error400(exc, request):
Expand All @@ -54,7 +51,7 @@ def error400(exc, request):
message = _(
'permission-denial',
default="Permission to '${permission}' ${context} was denied.",
mapping={'context':request.context.__class__.__name__,
mapping={'context': request.context.__class__.__name__,
'permission': exc.result.permission})
emeta = exc.result # TODO: log this as detail info
else:
Expand Down
28 changes: 25 additions & 3 deletions spynl/main/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,33 @@ class SpynlException(Exception):
def __init__(self, message='an internal error has occured'):
self.message = message

def __str__(self):
def make_response(self):
"""
Return a response as a dictionary.
If an exception needs to store additional information in the reponse
it can be overriden in the following way.
>>> def make_reponse(self):
data = super().make_response()
data.update({
'extra': 'Some extra information'
})
return data
"""
This will return a str version of the message. If the message is a
response = {
'status': 'error',
'type': self.__class__.__name__,
'message': self.message
}

return response

def __str__(self):
"""This will return a str version of the message. If the message is a
SpynlTranslationString, it will return an interpolated version of the
default (no translation). """
default (no translation).
"""
return str(self.message)


Expand Down
48 changes: 48 additions & 0 deletions spynl/tests/test_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Test functions from spynl.main."""
import pytest

from spynl.main.exceptions import SpynlException


@pytest.fixture
def exception_app(app_factory, settings, monkeypatch):
"""Plugin an endpoint that always raises and echo's back information."""

def patched_plugin_main(config):
def echo_raise(request):
"""
Always raises.
If some information is passes in the query params it is included
within the reponse.
"""
if request.GET:
class CustomException(SpynlException):
def make_response(self):
data = super().make_response()
data.update(request.GET)
return data

raise CustomException
raise SpynlException

config.add_endpoint(echo_raise, 'echo-raise')

# monkeypatch spynl.main.plugins.main as it is a simple entry point without
# internal logic where normally external plugins would get included.
monkeypatch.setattr('spynl.main.plugins.main', patched_plugin_main)
app = app_factory(settings)

return app


def test_spynlexception(exception_app):
"""Test regular SpynlException"""
response = exception_app.get('/echo-raise', expect_errors=True)
assert response.json == SpynlException().make_response()


def test_overridden_spynlexception(exception_app):
"""Test overridden SpynlException"""
response = exception_app.get('/echo-raise', params={'custom': 'blah'}, expect_errors=True)
assert response.json.get('custom') == 'blah'

0 comments on commit e95ac2d

Please sign in to comment.