KubiRoazhon
KubiRoazhon

Reputation: 1839

How to skip symfony2 warning message

I try to introduce an error 500 header in the catch part of the try. I'm calling my API method in a symfony controller method via Ajax. My issue is that I can't display the error message because of this symfony warning page

enter image description here

Here's the API side code :

try{
           //Some code 

        }catch (Exception $e){
            header('HTTP/1.1 500 Internal Server Error');
            die($e->getMessage()); // I get this message in the error part of the ajax call
        }

Controller method :

public function createModelAction(){

    $mail = "test";
    $pass = "test";

    $oauth = new OAuth($mail, $pass);

    $baseUrl = $this->container->getParameter('url_api');

    // Récupération des rapports d'avis du réseau demandé
    $link = "{$baseUrl}model/" . $mail . "/" . $pass . "/test";

    $oauth->fetch($link, $_REQUEST, OAUTH_HTTP_METHOD_POST);
    $response = json_decode($oauth->getLastResponse(), true);

    if ($response['error']) {
        return new JsonResponse($response['error']);
    } else {
        return new JsonResponse($response['model']);
    }
}

So any alternative to skip this problem.

Thanks

Upvotes: 2

Views: 146

Answers (1)

chalasr
chalasr

Reputation: 13167

Use Symfony Request and Response rather than use header manually.

Change the code from your API to :

} catch (Exception $e) {
    $response['error'] = $e->getMessage();

    return $response;
}

In your controller, define the status code of your response :

if ($response['error']) {
    return new JsonResponse($response['error'], 500);
}

You can also look for create an ExceptionListener and return a JsonResponse instead of throw the exception.

Upvotes: 2

Related Questions