1

I made a controller to provide some webservices in JSON and i would like to provide some errors informations when Symfony throw an exception ( Error 500 ) , how can i write such a thing ?

The main purpose of the webservice is to update informations in Symfony DB provided by the caller in POST values.

in my controller i return response in JSON and i would like to handle Symfony exception ( like when the values provided or not fitting the schema designed ) to return details informations about errors .

i thought about making a test of every values but it would be a long time to write and not e easy code to read or using a try / catch system , but i think Symfony already provide such a function .

What do you think ?

Thx :)

2 Answers 2

1

I think you should use an EventListener to catch errors and return the proper response.

You can place it inside your SomethingBundle/EventListener folder and also you need to define a service in order to be loaded by Symfony.

More info: Event Listener

I hope I helped you, if you think I might be wrong, let me know. Good luck!

EDIT

If you only want to catch the errors inside a specific controller (for example) a controller called Webservice inside your SomethingBundle, you must check it before doing anything:

public function onKernelException(GetResponseForExceptionEvent $event)
{       
    $request = $event->getRequest();

    if($this->getBundle($request) == "Something" && $this->getController($request) == "Webservice")
    {
        // Do your magic
        //...
    }
}

private function getBundle(Request $request)
{
    $pattern = "#([a-zA-Z]*)Bundle#";
    $matches = array();
    preg_match($pattern, $request->get('_controller'), $matches);

    return (count($matches)) ? $matches[0] : null;
}

private function getController(Request $request)
{
    $pattern = "#Controller\\\([a-zA-Z]*)Controller#";
    $matches = array();
    preg_match($pattern, $request->get('_controller'), $matches);

    return (count($matches)) ? $matches[1] : null;
}

DANGER This code is not tested, is only an approach for you to build your own code. But, if I have something wrong on it, tell me. I'd like to keep my examples clean.

Sign up to request clarification or add additional context in comments.

2 Comments

yes ! that's a good idea, but how can i use this eventlistener to catch exception only for my controller (my webservice controller )??
I did something similar months ago. Let me check my code and bring you an example. I'll edit my answer.
0

Use JsonResponse Symfony class in sandbox:

use Symfony\Component\HttpFoundation\JsonResponse;


$data = array(); // array of returned response, which encode to JSON
$data['error_message'] = 'Bad request or your other error...'); 

$response = new JsonResponse($data, 500); // 500 - response status
return $response;

1 Comment

it s not i was looking for, i was more after a function to detect exception throw by symfony without the exception template or simply detect when symfony throw an exception in order to return a bad status response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.