Skip to content

Commit

Permalink
create exceptions module
Browse files Browse the repository at this point in the history
--HG--
extra : convert_revision : eduardo%40eduardo-laptop-20100427152644-5ksuie1mstcgjz5q
  • Loading branch information
schettino72 committed Apr 27, 2010
1 parent 71a4571 commit 6af06f0
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 61 deletions.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
0.8
------

. task cleanup property
. deprecate setup objects

. support for depedency scanner (#218303)
Expand Down
52 changes: 0 additions & 52 deletions doit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,3 @@
"""

__version__ = (0,8,'dev')


import sys
import traceback


class CatchedException(object):
"""This used to save info from catched exceptions
The traceback from the original exception is saved
"""
def __init__(self, msg, exception=None):
self.message = msg
self.traceback = ''

if exception is not None:
self.traceback = traceback.format_exception(
exception.__class__, exception, sys.exc_info()[2])

#FIXME support exception parameter to be a CatchedException

def get_msg(self):
return "%s\n%s" % (self.message, "".join(self.traceback))

def get_name(self):
return self.__class__.__name__

def __repr__(self):
return "(<%s> %s)" % (self.get_name(), self.message)

def __str__(self):
return "%s\n%s" % (self.get_name(), self.get_msg())


# TODO rename this? should be ActionFailed?
class TaskFailed(CatchedException):
"""Task execution was not successful."""
pass


class TaskError(CatchedException):
"""Error while trying to execute task."""
pass


class SetupError(CatchedException):
"""Error while trying to execute setup object"""
pass


class DependencyError(CatchedException):
"""Error while trying to check if task is up-to-date"""
pass
2 changes: 1 addition & 1 deletion doit/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
from threading import Thread

from doit import TaskFailed, TaskError
from doit.exceptions import TaskFailed, TaskError

# Exceptions
class InvalidTask(Exception):
Expand Down
53 changes: 53 additions & 0 deletions doit/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys
import traceback


class CatchedException(object):
"""This used to save info from catched exceptions
The traceback from the original exception is saved
"""
def __init__(self, msg, exception=None):
self.message = msg
self.traceback = ''

if isinstance(exception, CatchedException):
self.traceback = exception.traceback
elif exception is not None:
self.traceback = traceback.format_exception(
exception.__class__, exception, sys.exc_info()[2])


def get_msg(self):
"""return full exception description (includes traceback)"""
return "%s\n%s" % (self.message, "".join(self.traceback))

def get_name(self):
return self.__class__.__name__

def __repr__(self):
return "(<%s> %s)" % (self.get_name(), self.message)

def __str__(self):
return "%s\n%s" % (self.get_name(), self.get_msg())


class TaskFailed(CatchedException):
"""Task execution was not successful."""
pass


class TaskError(CatchedException):
"""Error while trying to execute task."""
pass


class SetupError(CatchedException):
"""Error while trying to execute setup object"""
pass


class DependencyError(CatchedException):
"""Error while trying to check if task is up-to-date"""
pass


5 changes: 3 additions & 2 deletions doit/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import sys

from doit import CatchedException, TaskFailed, SetupError, DependencyError
from doit.exceptions import CatchedException
from doit.exceptions import TaskFailed, SetupError, DependencyError
from doit.dependency import Dependency


Expand Down Expand Up @@ -190,7 +191,7 @@ def teardown(self):
catched = task.execute_teardown(sys.stdout, sys.stderr,
self.verbosity)
if catched:
error = SetupError("ERROR on setup_obj cleanup", catched)
error = SetupError("ERROR on teardown action", catched)
self.reporter.cleanup_error(error)


Expand Down
3 changes: 2 additions & 1 deletion doit/task.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import types
import os

from doit import CatchedException
from doit import cmdparse
from doit.exceptions import CatchedException
from doit.action import create_action, InvalidTask


class Task(object):
"""Task
Expand Down
2 changes: 1 addition & 1 deletion tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import py.test

from doit import action
from doit import TaskError, TaskFailed
from doit.exceptions import TaskError, TaskFailed

#path to test folder
TEST_PATH = os.path.dirname(__file__)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from doit import exceptions


class TestCatchedException(object):
def test_name(self):
class XYZ(exceptions.CatchedException):
pass
my_excp = XYZ("hello")
assert 'XYZ' == my_excp.get_name()
assert 'XYZ' in str(my_excp)
assert 'XYZ' in repr(my_excp)

def test_msg_notraceback(self):
my_excp = exceptions.CatchedException('got you')
msg = my_excp.get_msg()
assert 'got you' in msg

def test_exception(self):
try:
raise IndexError('too big')
except Exception, e:
my_excp = exceptions.CatchedException('got this', e)
msg = my_excp.get_msg()
assert 'got this' in msg
assert 'too big' in msg
assert 'IndexError' in msg

def test_catched(self):
try:
raise IndexError('too big')
except Exception, e:
my_excp = exceptions.CatchedException('got this', e)
my_excp2 = exceptions.CatchedException('handle that', my_excp)
msg = my_excp2.get_msg()
assert 'handle that' in msg
assert 'got this' not in msg # could be there too...
assert 'too big' in msg
assert 'IndexError' in msg


class TestAllCatched(object):
def test(self):
assert issubclass(exceptions.TaskFailed, exceptions.CatchedException)
assert issubclass(exceptions.TaskError, exceptions.CatchedException)
assert issubclass(exceptions.SetupError, exceptions.CatchedException)
assert issubclass(exceptions.DependencyError,
exceptions.CatchedException)
2 changes: 1 addition & 1 deletion tests/test_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from doit import reporter
from doit.task import Task
from doit import CatchedException
from doit.exceptions import CatchedException
from doit.dependency import json #FIXME move json import to __init__.py


Expand Down
4 changes: 2 additions & 2 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import py.test

from doit import TaskError
from doit import CatchedException
from doit.exceptions import TaskError
from doit.exceptions import CatchedException
from doit import action
from doit import task

Expand Down

0 comments on commit 6af06f0

Please sign in to comment.