SensacionRC
SensacionRC

Reputation: 615

Symfony 3 handling errors

Is there a way to handle all errors, from my symfony controllers, for example, if I get this error:

enter image description here

In my controller is there a way using try/catch to get this error?. For example:

class SomeClass extends Controller
{
    public function doSomethingAction(Request $request){
        //do something
        try {
            //do something
        }
        catch(\Exception $e){
            dump("ERROR:".$e->getMessage()); //<--this is not dumping anithing
        }
    }
}

I get allways the red screen message in the network call preview: enter image description here

Instead of something like:

"ERROR: Type error: Argument 1 passed to.....

Upvotes: 1

Views: 1106

Answers (1)

dbrumann
dbrumann

Reputation: 17166

With PHP 7 you are able to handle PHP errors like TypeErrors from mismatching types (like in your example) as well as exceptions by catching their shared interface Throwable.

You should be careful with this, especially outside controllers, as this could prevent you from seeing unexpected errors which can lead to problems down the line or you not seeing when parts of your application are entirely broken. At the very least you should have proper logging in place.

In summary, you can catch errors along with exception like this:

try {
    ...
} catch (\Throwable $error) {
    ...
}

Upvotes: 3

Related Questions