Okay, so now let's test the code that we have already created by registering this error handler to
handle errors and exceptions in the PHP app.
And then we're going to be working on the missing parts like logging the error into an actual log file.
So let's jump to the bootstrap file.
This is our setup file that sets up our whole application in the command line.
And when it works as a web server, one of the first things that we should do is to register the errors.
So you probably remember that we have those two functions set exception handler and set error handler.
So you've seen this trick before that if your callback is a method on a class, then you just pass it
as an array.
So we have our error handler class, which as you see gets imported here automatically.
And it has this class constant.
This gives the full class name including namespace.
And the method here is handle exception.
Now I can just copy and paste that.
And this here is handle error.
So I think we might even register the error handling first and only then try to load the config.
Now let's try this out someplace shall we.
So since currently we only have the error handling that is working in the CLI environment, we need
to jump to our only CLI command.
Now as you see the logic of this command is wrapped in a try catch block.
And the exception handlers in PHP they catch an Catched exceptions.
However, that sounds so.
If you don't handle exceptions explicitly using a try catch block, then it will be handled by our global
handler.
Now I just want to simulate the error here because this command works.
I don't expect anything to go wrong here.
That's why I'll just throw an exception saying testing, exception handling.
And I expect this to just show up in the command line.
Now let me open the terminal and type composer schema load.
So this is our error.
As we expected there is a date.
The color is red.
And then we have a default one which is white.
In my case because I'm using a black terminal.
So we have all the information the class name, the actual message, and also where the error happened
and on which line.
So if you are running this inside VSCode, you should be able to command click in Mac or Control.
Click on it on windows, which should take you directly to the file and line where the error has happened.
As you see, I've just clicked that and it takes me to this file.
Now also, since we have returned a non-zero code, the terminal itself is letting us know that it returned
an error code.
Additionally, we see a stack trace, which is super simple because in this command line script we don't
even call any functions, we just throw an exception right away.
The next step in our error handling is to store those errors inside a log file, so that everything
that has happened can be later reviewed.