I
I
Igor2019-09-17 16:20:41
symfony
Igor, 2019-09-17 16:20:41

At what level in Symfony to catch errors?

Colleagues, good evening!
At what level in Symfony to catch errors so that the server always responds with valid JSON.
The idea is that for any outcome, the client always received JSON of a certain structure.
For example:
/database.getCityById?id=3538

{
    "data": {
        "id": 3538,
        "name": "Воронеж",
        "region": {
            "id": 3529,
            "name": "Воронежская обл."
        },
        "country": {
            "name": "Россия",
            "id": 3159
        }
    },
    "code": 0
}

But at some point, something happened, for example, the database gave a wedge or something else.
The framework intercepts the error.
The information is written to the log, and to the client something like
{
  "code": 1,
  "msg": "unknown error"
}

I need this in order for Nuxt to always respond correctly to these kinds of errors.
My first option:
Interception at the top level
try {
    $response = $kernel->handle($request);
} catch (Exception $e) {
    $response = new JsonResponse(["code" => 1]);
    $response->send();
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question