How to use the masonite.exceptions.ContainerError function in masonite

To help you get started, we’ve selected a few masonite examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MasoniteFramework / masonite / tests / core / test_container.py View on Github external
def test_container_raises_value_error(self):
        with self.assertRaises(ContainerError):
            self.assertTrue(self.app.resolve(self._function_not_in_container))
github MasoniteFramework / masonite / tests / core / test_container.py View on Github external
def test_raises_error_when_getting_instances_of_classes(self):
        with self.assertRaises(ContainerError):
            self.assertTrue(self.app.resolve(self._function_test_find_method_on_similiar_objects))
github MasoniteFramework / masonite / masonite / app.py View on Github external
"""
        objects = []
        passing_arguments = list(resolving_arguments)
        if self.remember and obj in self._remembered:
            objects = self._remembered[obj]
            try:
                return obj(*objects)
            except TypeError as e:
                raise ContainerError(str(e))
        elif self.remember and not passing_arguments and inspect.ismethod(obj) and "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__) in self._remembered:
            location = "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__)
            objects = self._remembered[location]
            try:
                return obj(*objects)
            except TypeError as e:
                raise ContainerError(str(e))
        else:
            for _, value in self.get_parameters(obj):
                if ':' in str(value):
                    param = self._find_annotated_parameter(value)
                    if inspect.isclass(param):
                        param = self.resolve(param)
                    objects.append(param)
                elif 'self' in str(value):
                    objects.append(obj)
                elif '=' in str(value):
                    objects.append(value.default)
                elif '*' in str(value):
                    continue
                elif self.resolve_parameters:
                    objects.append(self._find_parameter(value))
                elif resolving_arguments:
github MasoniteFramework / masonite / masonite / app.py View on Github external
if inspect.isclass(param):
                        param = self.resolve(param)
                    objects.append(param)
                elif 'self' in str(value):
                    objects.append(obj)
                elif '=' in str(value):
                    objects.append(value.default)
                elif '*' in str(value):
                    continue
                elif self.resolve_parameters:
                    objects.append(self._find_parameter(value))
                elif resolving_arguments:
                    try:
                        objects.append(passing_arguments.pop(0))
                    except IndexError:
                        raise ContainerError('Not enough dependencies passed. Resolving object needs {} dependencies.'.format(len(inspect.signature(obj).parameters)))
                else:
                    raise ContainerError(
                        "This container is not set to resolve parameters. You can set this in the container"
                        " constructor using the 'resolve_parameters=True' keyword argument.")
        try:
            if self.remember:
                if not inspect.ismethod(obj):
                    self._remembered[obj] = objects
                else:
                    signature = "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__)
                    self._remembered[signature] = objects
            return obj(*objects)
        except (TypeError,) as e:
            import sys
            import traceback
            exception = ContainerError(str(e))
github MasoniteFramework / masonite / masonite / app.py View on Github external
else:
                    raise ContainerError(
                        "This container is not set to resolve parameters. You can set this in the container"
                        " constructor using the 'resolve_parameters=True' keyword argument.")
        try:
            if self.remember:
                if not inspect.ismethod(obj):
                    self._remembered[obj] = objects
                else:
                    signature = "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__)
                    self._remembered[signature] = objects
            return obj(*objects)
        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
github MasoniteFramework / masonite / masonite / app.py View on Github external
Raises:
            ContainerError -- Thrown when the dependency is not found in the container.

        Returns:
            object -- Returns the object found in the container
        """
        parameter = str(keyword)

        if parameter != 'self' and parameter in self.providers:
            obj = self.providers[parameter]
            self.fire_hook('resolve', parameter, obj)
            return obj
        elif '=' in parameter:
            return keyword.default

        raise ContainerError(
            'The parameter dependency with the key of {0} could not be found in the container'.format(
                parameter)
        )
github MasoniteFramework / masonite / masonite / app.py View on Github external
objects.append(param)
                elif 'self' in str(value):
                    objects.append(obj)
                elif '=' in str(value):
                    objects.append(value.default)
                elif '*' in str(value):
                    continue
                elif self.resolve_parameters:
                    objects.append(self._find_parameter(value))
                elif resolving_arguments:
                    try:
                        objects.append(passing_arguments.pop(0))
                    except IndexError:
                        raise ContainerError('Not enough dependencies passed. Resolving object needs {} dependencies.'.format(len(inspect.signature(obj).parameters)))
                else:
                    raise ContainerError(
                        "This container is not set to resolve parameters. You can set this in the container"
                        " constructor using the 'resolve_parameters=True' keyword argument.")
        try:
            if self.remember:
                if not inspect.ismethod(obj):
                    self._remembered[obj] = objects
                else:
                    signature = "{}.{}.{}".format(obj.__module__, obj.__self__.__class__.__name__, obj.__name__)
                    self._remembered[signature] = objects
            return obj(*objects)
        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]