Repository: tailhook/injections

Issue:
Use custom exception for injecting container into object with unknown dependencies
I have a class
```
@has
class A:
    redis = depends(Redis)
    loop = depends(AbstractLoop)
```

When I try to initialize it from container with missing dependencies I have a `KeyError`.
```
inj = Container()
inj['redis'] = create_redis()
inj.inject(A())  # raises KeyError
```

Exception on injection stage is pretty cool but I suggest using custom exception class. Not `KeyError` but derived from it. Exception text also should be more informative than just `KeyError: 'loop'`.

Discussion:

Patch:
diff --git a/injections/__init__.py b/injections/__init__.py
index 47bdc0d..dce8a9e 100644
--- a/injections/__init__.py
+++ b/injections/__init__.py
@@ -1,6 +1,7 @@
 from .core import (
     Container,
     Dependency,
+    MissingDependencyError,
     has,
     depends,
     propagate,
@@ -11,6 +12,7 @@ from .core import (
 __all__ = [
     'Container',
     'Dependency',
+    'MissingDependencyError',
     'has',
     'depends',
     'propagate',
diff --git a/injections/core.py b/injections/core.py
index a4dccae..edff1db 100644
--- a/injections/core.py
+++ b/injections/core.py
@@ -76,6 +76,13 @@ class Dependency:
 depends = Dependency  # nicer declarative name
 
 
+class MissingDependencyError(KeyError):
+    """Required dependency is missed in container"""
+
+    def __str__(self):
+        return "Dependency {!r} is missed in container".format(self.args[0])
+
+
 class Container(object):
     """Container for things that will be dependency-injected
 
@@ -104,7 +111,10 @@ class Container(object):
         deps = getattr(inst, '__injections__', None)
         if deps:
             for attr, dep in deps.items():
-                val = pro[dep.name]
+                val = pro.get(dep.name)
+                if val is None:
+                    raise MissingDependencyError(dep.name)
+
                 if not isinstance(val, dep.type):
                     raise TypeError("Wrong provider for {!r}".format(val))
                 setattr(inst, attr, val)
diff --git a/setup.py b/setup.py
index 41cf797..c1e7d04 100644
--- a/setup.py
+++ b/setup.py
@@ -16,4 +16,5 @@ setup(name='injections',
         'Programming Language :: Python :: 3',
         'Programming Language :: Python :: 2',
         ],
+      license='MIT',
      )


Test patch:
diff --git a/injections/test_core.py b/injections/test_core.py
index 2e9aabe..06006cd 100644
--- a/injections/test_core.py
+++ b/injections/test_core.py
@@ -126,3 +126,21 @@ class TestCore(TestCase):
         c['name'] = 1
         with self.assertRaises(TypeError):
             c.inject(self.Consumer())
+
+    def test_missing_dependency(self):
+        c = di.Container()
+        c['a'] = 1
+
+        @di.has
+        class Consumer:
+            a = di.depends(int)
+            b = di.depends(int)
+
+        if hasattr(self, 'assertRaisesRegex'):
+            checker = self.assertRaisesRegex
+        else:
+            checker = self.assertRaisesRegexp
+
+        with checker(di.MissingDependencyError,
+                     "Dependency 'b' is missed in container"):
+            c.inject(Consumer())