1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
"""Spy-related errors."""
from __future__ import unicode_literals
import traceback
class InternalKGBError(Exception):
"""An internal error about the inner workings of KGB."""
def __init__(self, msg):
"""Initialize the error.
Args:
msg (unicode):
The message to display. A general message about contacting
support will be appended to this.
"""
super(InternalKGBError, self).__init__(
'%s\n\n'
'This is an internal error in KGB. Please report it!'
% msg)
class ExistingSpyError(ValueError):
"""An error for when an existing spy was found on a function.
This will provide a helpful error message explaining what went wrong,
showing a backtrace of the original spy's setup in order to help diagnose
the problem.
"""
def __init__(self, func):
"""Initialize the error.
Args:
func (callable):
The function containing an existing spy.
"""
super(ExistingSpyError, self).__init__(
'The function %(func)r has already been spied on. Here is where '
'that spy was set up:\n\n'
'%(stacktrace)s\n'
'You may have encountered a crash in that test preventing the '
'spy from being unregistered. Try running that test manually.'
% {
'func': func,
'stacktrace': ''.join(traceback.format_stack(
func.spy.init_frame.f_back)[-4:]),
})
class IncompatibleFunctionError(ValueError):
"""An error for when a function signature is incompatible.
This is used for the ``call_fake`` function passed in when setting up a
spy.
"""
def __init__(self, func, func_sig, incompatible_func,
incompatible_func_sig):
"""Initialize the error.
Args:
func (callable):
The function containing the original signature.
func_sig (kgb.signature.FunctionSig):
The signature of ``func``.
incompatible_func (callable):
The function that was not compatible.
incompatible_func_sig (kgb.signature.FunctionSig):
The signature of ``incompatible_func``.
"""
super(IncompatibleFunctionError, self).__init__(
'The function signature of %r (%s) is not compatible with %r (%s).'
% (incompatible_func,
incompatible_func_sig.format_arg_spec(),
func,
func_sig.format_arg_spec()))
class UnexpectedCallError(AssertionError):
"""A call was made to a spy that was not expected."""
|