From 6b6e6b1cab7a633faf16631a565ecb6988dadb48 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Sat, 3 Jan 2009 17:27:53 -0700 Subject: Ported plugin registration errors into errors2.py; plugable.Registrar now raises new errors2 exceptions --- ipalib/errors2.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++------ ipalib/plugable.py | 21 ++++++++++---- 2 files changed, 90 insertions(+), 14 deletions(-) (limited to 'ipalib') diff --git a/ipalib/errors2.py b/ipalib/errors2.py index 676dd79f3..7793a9146 100644 --- a/ipalib/errors2.py +++ b/ipalib/errors2.py @@ -43,7 +43,7 @@ import request class PrivateError(StandardError): """ - Base class for exceptions that are *never* returned in an RPC response. + Base class for exceptions that are *never* forwarded in an RPC response. """ format = '' @@ -72,15 +72,82 @@ class SubprocessError(PrivateError): 1 >>> e.argv ('/bin/false',) + >>> e.message + "return code 1 from ('/bin/false',)" >>> str(e) "return code 1 from ('/bin/false',)" """ + format = 'return code %(returncode)d from %(argv)r' +class PluginSubclassError(PrivateError): + """ + Raised when a plugin doesn't subclass from an allowed base. + + For example: + + >>> raise PluginSubclassError(plugin='bad', bases=('base1', 'base2')) + Traceback (most recent call last): + ... + PluginSubclassError: 'bad' not subclass of any base in ('base1', 'base2') + + """ + + format = '%(plugin)r not subclass of any base in %(bases)r' + + +class PluginDuplicateError(PrivateError): + """ + Raised when the same plugin class is registered more than once. + + For example: + + >>> raise PluginDuplicateError(plugin='my_plugin') + Traceback (most recent call last): + ... + PluginDuplicateError: 'my_plugin' was already registered + """ + + format = '%(plugin)r was already registered' + + +class PluginOverrideError(PrivateError): + """ + Raised when a plugin overrides another without using ``override=True``. + + For example: + + >>> raise PluginOverrideError(base='Command', name='env', plugin='my_env') + Traceback (most recent call last): + ... + PluginOverrideError: unexpected override of Command.env with 'my_env' + """ + + format = 'unexpected override of %(base)s.%(name)s with %(plugin)r' + + +class PluginMissingOverrideError(PrivateError): + """ + Raised when a plugin overrides another that has not been registered. + + For example: + + >>> raise PluginMissingOverrideError(base='Command', name='env', plugin='my_env') + Traceback (most recent call last): + ... + PluginMissingOverrideError: Command.env not registered, cannot override with 'my_env' + """ + + format = '%(base)s.%(name)s not registered, cannot override with %(plugin)r' + + + +############################################################################## +# Public errors: class PublicError(StandardError): """ - **900** Base class for exceptions that can be returned in an RPC response. + **900** Base class for exceptions that can be forwarded in an RPC response. """ code = 900 @@ -96,8 +163,6 @@ class PublicError(StandardError): - - class InternalError(PublicError): """ **901** Used to conceal a non-public exception. @@ -108,7 +173,7 @@ class InternalError(PublicError): ############################################################################## -# 1000 - 1999: Authentication Errors +# 1000 - 1999: Authentication errors class AuthenticationError(PublicError): """ **1000** Base class for authentication errors (*1000 - 1999*). @@ -119,7 +184,7 @@ class AuthenticationError(PublicError): ############################################################################## -# 2000 - 2999: Authorization Errors +# 2000 - 2999: Authorization errors class AuthorizationError(PublicError): """ **2000** Base class for authorization errors (*2000 - 2999*). @@ -130,7 +195,7 @@ class AuthorizationError(PublicError): ############################################################################## -# 3000 - 3999: Invocation Errors +# 3000 - 3999: Invocation errors class InvocationError(PublicError): """ @@ -201,7 +266,7 @@ class ValidationError(InvocationError): ############################################################################## -# 4000 - 4999: Execution Errors +# 4000 - 4999: Execution errors class ExecutionError(PublicError): """ @@ -213,7 +278,7 @@ class ExecutionError(PublicError): ############################################################################## -# 5000 - 5999: Generic Errors +# 5000 - 5999: Generic errors class GenericError(PublicError): """ diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 4ef2135b5..094634d33 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -34,6 +34,7 @@ import os from os import path import subprocess import errors +import errors2 from config import Env import util from base import ReadOnly, NameSpace, lock, islocked, check_name @@ -461,7 +462,9 @@ class Registrar(DictProxy): found = True yield (base, sub_d) if not found: - raise errors.SubclassError(klass, self.__allowed.keys()) + raise errors2.PluginSubclassError( + plugin=klass, bases=self.__allowed.keys() + ) def __call__(self, klass, override=False): """ @@ -471,11 +474,11 @@ class Registrar(DictProxy): :param override: If true, override an already registered plugin. """ if not inspect.isclass(klass): - raise TypeError('plugin must be a class: %r' % klass) + raise TypeError('plugin must be a class; got %r' % klass) # Raise DuplicateError if this exact class was already registered: if klass in self.__registered: - raise errors.DuplicateError(klass) + raise errors2.PluginDuplicateError(plugin=klass) # Find the base class or raise SubclassError: for (base, sub_d) in self.__findbases(klass): @@ -483,11 +486,19 @@ class Registrar(DictProxy): if klass.__name__ in sub_d: if not override: # Must use override=True to override: - raise errors.OverrideError(base, klass) + raise errors2.PluginOverrideError( + base=base.__name__, + name=klass.__name__, + plugin=klass, + ) else: if override: # There was nothing already registered to override: - raise errors.MissingOverrideError(base, klass) + raise errors2.PluginMissingOverrideError( + base=base.__name__, + name=klass.__name__, + plugin=klass, + ) # The plugin is okay, add to sub_d: sub_d[klass.__name__] = klass -- cgit