Skip to content

Commit

Permalink
Updated example error handling
Browse files Browse the repository at this point in the history
- Included examples of what exceptions may be thrown and why
  • Loading branch information
tlmcclatchey committed Jul 2, 2023
1 parent dcb3253 commit 6812f58
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 29 deletions.
11 changes: 6 additions & 5 deletions examples/call.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ function exampleFunction(string $message)

$injector = new DependencyInjector();

// Call the function 'exampleFunction' with 'Hello, world!' as the parameter
try {
// Call the function 'exampleFunction' with 'Hello, world!' as the parameter
$injector->call('exampleFunction', ['message' => 'Hello, world!']);
} catch (Exception $e) {
// Handle exceptions
echo "An error occurred: " . $e->getMessage();
}
} catch (CommonPHP\DependencyInjection\Exceptions\CallFailedException $e) {
// This exception is thrown when an invocation of a method or function fails.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}
35 changes: 28 additions & 7 deletions examples/instantiate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,34 @@ public function getProperty(): string

$injector = new DependencyInjector();

// Instantiate ExampleClass with a provided constructor parameter
try {
// Instantiate ExampleClass with a provided constructor parameter
$instance = $injector->instantiate(ExampleClass::class, ['property' => 'exampleValue']);

// Use the instantiated object
echo $instance->getProperty(); // Outputs: exampleValue
} catch (Exception $e) {
// Handle exceptions
echo "An error occurred: " . $e->getMessage();
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotDefinedException $e) {
// This exception is thrown when a class is not defined but attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotInstantiableException $e) {
// This exception is thrown when a class that is not instantiable (like an interface or abstract class) is attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiateCircularReferenceException $e) {
// This exception is thrown when there is a circular reference detected during instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiationFailedException $e) {
// This exception is thrown when instantiation of a class fails for some reason.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ParameterDiscoveryFailedException $e) {
// This exception is thrown when there's a failure in discovering the parameters required for instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\UnsupportedReflectionTypeException $e) {
// This exception is thrown when there's an unsupported reflection type encountered during parameter discovery.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}

// Use the instantiated object
echo $instance->getProperty(); // Outputs: exampleValue
56 changes: 47 additions & 9 deletions examples/invoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,55 @@ public function getProperty(): string
}

$injector = new DependencyInjector();

// Instantiate ExampleClass without any constructor parameters
try {
// Instantiate ExampleClass without any constructor parameters
$instance = $injector->instantiate(ExampleClass::class, []);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotDefinedException $e) {
// This exception is thrown when a class is not defined but attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotInstantiableException $e) {
// This exception is thrown when a class that is not instantiable (like an interface or abstract class) is attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiateCircularReferenceException $e) {
// This exception is thrown when there is a circular reference detected during instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiationFailedException $e) {
// This exception is thrown when instantiation of a class fails for some reason.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ParameterDiscoveryFailedException $e) {
// This exception is thrown when there's a failure in discovering the parameters required for instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\UnsupportedReflectionTypeException $e) {
// This exception is thrown when there's an unsupported reflection type encountered during parameter discovery.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}

// Invoke setProperty method on the instantiated object
// Invoke setProperty method on the instantiated object
try {
$injector->invoke($instance, 'setProperty', ['newValue' => 'Updated value']);

// Use the updated object
echo $instance->getProperty(); // Outputs: Updated value
} catch (Exception $e) {
// Handle exceptions
echo "An error occurred: " . $e->getMessage();
} catch (\CommonPHP\DependencyInjection\Exceptions\InvocationFailedException $e) {
// This exception is thrown when an invocation of a method or function fails.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodIsStaticException $e) {
// This exception is thrown when an attempt is made to invoke a static method as if it was non-static.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodNotDefinedException $e) {
// This exception is thrown when an attempt is made to invoke a method that is not defined.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\MethodNotPublicException $e) {
// This exception is thrown when an attempt is made to invoke a method that is not public.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}

// Use the updated object
echo $instance->getProperty(); // Outputs: Updated value
29 changes: 25 additions & 4 deletions examples/lookup-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,33 @@ public function __construct(public string $message)
return null;
});

// Create a new instance of ExampleClass using the custom lookup hook
try {
// Create a new instance of ExampleClass using the custom lookup hook
$instance = $injector->instantiate(ExampleClass::class, []);
} catch (Exception $e) {
// Handle exceptions
echo "An error occurred: " . $e->getMessage();
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotDefinedException $e) {
// This exception is thrown when a class is not defined but attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ClassNotInstantiableException $e) {
// This exception is thrown when a class that is not instantiable (like an interface or abstract class) is attempted to be instantiated.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiateCircularReferenceException $e) {
// This exception is thrown when there is a circular reference detected during instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\InstantiationFailedException $e) {
// This exception is thrown when instantiation of a class fails for some reason.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\ParameterDiscoveryFailedException $e) {
// This exception is thrown when there's a failure in discovering the parameters required for instantiation of a class.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
} catch (\CommonPHP\DependencyInjection\Exceptions\UnsupportedReflectionTypeException $e) {
// This exception is thrown when there's an unsupported reflection type encountered during parameter discovery.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}

echo $instance->message; // Outputs: "Hello from custom lookup hook!"
9 changes: 5 additions & 4 deletions examples/populate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class ExampleClass
// Create a new instance of ExampleClass
$instance = new ExampleClass();

// Populate the properties of the instance
try {
// Populate the properties of the instance
$injector->populate($instance, ['message' => 'Hello, world!']);
} catch (Exception $e) {
// Handle exceptions
echo "An error occurred: " . $e->getMessage();
} catch (\CommonPHP\DependencyInjection\Exceptions\UnsupportedReflectionTypeException $e) {
// This exception is thrown when there's an unsupported reflection type encountered during property discovery.
// Proper error message should be returned or logged, and the error should be handled appropriately.
die($e);
}

echo $instance->message; // Outputs: "Hello, world!"

0 comments on commit 6812f58

Please sign in to comment.