Skip to content

Commit

Permalink
Merge pull request #804 from MasoniteFramework/master
Browse files Browse the repository at this point in the history
Next Minor
  • Loading branch information
josephmancuso committed Jun 27, 2019
2 parents cab173c + e60379f commit 49dbaa3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion masonite/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'masonite'
__description__ = 'The core for the Masonite framework'
__url__ = 'https://github.com/MasoniteFramework/masonite'
__version__ = '2.2.2'
__version__ = '2.2.3'
__author__ = 'Joseph Mancuso'
__author_email__ = 'joe@masoniteproject.com'
__licence__ = 'MIT'
11 changes: 9 additions & 2 deletions masonite/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,15 @@ def resolve(self, obj, *resolving_arguments):
signature = "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__)
self._remembered[signature] = objects
return obj(*objects)
except TypeError as e:
raise ContainerError(str(e))
except (TypeError,) as e:
import sys
import traceback
exception = ContainerError(str(e))
exc_type, exc_obj, exc_tb = sys.exc_info()
exception.__class__.extras = [exc_type, exc_obj, exc_tb]
exception.__class__.tb = traceback.extract_tb(exc_tb)
exception.__class__.file = obj.__code__.co_filename
raise exception from e

def collect(self, search):
"""Fetch a dictionary of objects using a search query.
Expand Down
13 changes: 13 additions & 0 deletions masonite/exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,34 @@ def handle(self, _):
if not application.DEBUG:
return

exc_type, _, _ = sys.exc_info()
# return a view
if request.header('Content-Type') == 'application/json':
stacktrace = []
for stack in traceback.extract_tb(sys.exc_info()[2]):
stacktrace.append("{} line {} in {}".format(stack[0], stack[1], stack[2]))

if hasattr(exc_type, 'file'):
stacktrace.append("{} line {} in {}".format(exc_type.file, exc_type.tb[1], exc_type.tb[2]))

self.response.view({'error': {'exception': str(self._exception), 'status': 500, 'stacktrace': stacktrace}}, status=request.get_status())
return

if hasattr(exc_type, 'file'):
last_stacktrace = [[exc_type.file, exc_type.tb[-1][1], exc_type.tb[-1][2]]]
second_to_last_stacktrace = [[exc_type.file, exc_type.tb[-2][1], exc_type.tb[-2][2]]]
else:
last_stacktrace = []
second_to_last_stacktrace = []

self.response.view(self._app.make('View')('/masonite/snippets/exception',
{
'exception': self._exception,
'split_exception': str(self._exception).split(' '),
'traceback': traceback,
'tb': sys.exc_info(),
'stacktrace': traceback.extract_tb(sys.exc_info()[2]) + last_stacktrace + second_to_last_stacktrace,
'second_to_last': second_to_last_stacktrace,
'app': self._app,
'enumerate': enumerate,
'open': open,
Expand Down
8 changes: 6 additions & 2 deletions masonite/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ def _find_controller(self, controller):
# Set the controller method on class. This is a string
self.controller_method = mod[1]

except Exception as e:
print('\033[93mWarning in routes/web.py!', e, '\033[0m')
except Exception:
import sys
import traceback
exc_type, exc_obj, exc_tb = sys.exc_info()
tb = traceback.extract_tb(exc_tb)[-1]
print('\033[93mWarning in routes/web.py!', exc_type, 'in', tb[0], 'on line', tb[1], '\033[0m')

def get_response(self):
# Resolve Controller Constructor
Expand Down
22 changes: 11 additions & 11 deletions masonite/snippets/exception.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<ol class="breadcrumb" style="margin-bottom:1px; background-color: white;">
<li>
<span style="color: #eb6864">{{ tb[0].__name__ }} &gt; {{ exception }} in
{% for stack in traceback.extract_tb(tb[2])[::-1] %}
{% for stack in stacktrace[::-1] %}
{% if not 'site-packages' in stack[0] %}
<u><a style="color: inherit;" href="#{{stack[0]}}">{{ stack[0] }} on line {{ stack[1] }}</a></u>
{% break %}
Expand All @@ -54,29 +54,29 @@ <h1 style="margin-top:12px;margin-bottom:12px;">StackTrace</h1>
</div>
<div class="row">
<div class="col-md-12" style="background-color:#f5f5f5;padding-left:0px;margin-left:15px;padding-right:0px;">
{% for stack in traceback.extract_tb(tb[2]) %}
{% if not 'site-packages' in stack[0] %}
{% set local_file_background_color = '#d9ffc2' %}
{% else %}
{% for stack in stacktrace %}
{% if 'site-packages' in stack[0] %}
{% set local_file_background_color = 'inherit' %}
{% else %}
{% set local_file_background_color = '#d9ffc2' %}
{% endif %}
<div id="{{ stack[0] }}" class="stack-row trace" style="padding:15px;padding-bottom:7px;padding-top:15px;background-color: {{ local_file_background_color }}">
<div id="{{ stack[0] }}" class="stack-row trace" style="padding:15px;padding-bottom:7px;padding-top:15px;margin-bottom:20px;background-color: {{ local_file_background_color }} !important;">
<h4> {{ stack[0] }}, line {{ stack[1] }} in {{ stack[2]}} </h4>

{% for i, line in enumerate(open(stack[0])) %}
{% if stack[1] - 5 <= i <= stack[1] + 5 %}

{% if i == stack[1] - 1 %}
<div style="background-color: #f1c40f">
{{ i + 1 }}. {{ line }}
<div style="background-color: #f1c40f;">
{{ i + 1 }}. <span style="margin-left: {{ indent }}">{{ line }}</span>
</div>
{% elif stack[1] - 3 <= i <= stack[1] + 1 %}
{% elif stack[1] - 3 <= i <= stack[1] + 1 %}
<div style="background-color: #fee893;">
{{ i + 1 }}. {{ line }}
{{ i + 1 }}. <span style="margin-left: {{ indent }}">{{ line }}</span>
</div>
{% else %}
<div>
{{ i + 1 }}. {{ line }}
{{ i + 1 }}. <span style="margin-left: {{ indent }}">{{ line }}</span>
</div>
{% endif %}

Expand Down

0 comments on commit 49dbaa3

Please sign in to comment.