Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

Latest commit

 

History

History
60 lines (37 loc) · 1.88 KB

custom-exceptions.rst

File metadata and controls

60 lines (37 loc) · 1.88 KB

FaultManagerdoc

Custom Exceptions

Another powerful feature of Fault Manager is the ability to generate and compile exception classes on the fly.

Instead of spending time on building your own exception classes and end up with a pile of exception files here and there in your project, you can use Fault Manager and provide a name of an exception that has not been generated yet, and Fault Manager will do the rest. You actually don't even have to do it before you throw the exception, you can generate exception classes just at the point you want to use them.

Take the following as example:

Fault::throw('CustomError', 'a message here');

:phpCustomError class is not yet defined anywhere in our project, what Fault Manager will do is:

Check if a class with that name already exists

If it is
  1. Get a new instance of the class with params
If it is not
  1. Generate the class and compile it to file
  2. Load the newly generated class with params

The above example will produce the following file:

<?php

class CustomError extends Exception
{


}

The above file will be saved into the compiled path directory.

For more info on how to define your own path, check features.compiler.path.

Rules

There are few rules to follow in order to generate a custom exception class:

  1. If you pass an empty string as :php$exceptionClass a :phpExceptionNameIsEmpty exception will be thrown
  2. If you try to generate a namespaced custom exception a :phpNamespaceError exception will be thrown as that feature is not yet supported