> APIs >

Course Overview

Login to bookmark this course

Symfony RESTful API: Errors (Course 2)

Beat those 404's & 500's! Learn how to handle errors consistently across your Symfony RESTful API projects.

  • 2037 students
  • EN Captions
  • EN Script
  • Certificate of Completion

Your Guides

About this course

This tutorial uses an older version of Symfony. The concepts of REST and errors are still valid, but I recommend using API Platform in new Symfony apps.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.6.*", // v2.6.11
        "doctrine/orm": "~2.2,>=2.2.3,<2.5", // v2.4.7
        "doctrine/dbal": "<2.5", // v2.4.4
        "doctrine/doctrine-bundle": "~1.2", // v1.4.0
        "twig/extensions": "~1.0", // v1.2.0
        "symfony/assetic-bundle": "~2.3", // v2.6.1
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.8
        "symfony/monolog-bundle": "~2.4", // v2.7.1
        "sensio/distribution-bundle": "~3.0,>=3.0.12", // v3.0.21
        "sensio/framework-extra-bundle": "~3.0,>=3.0.2", // v3.0.7
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "hautelook/alice-bundle": "0.2.*", // 0.2
        "jms/serializer-bundle": "0.13.*" // 0.13.0
    },
    "require-dev": {
        "sensio/generator-bundle": "~2.3", // v2.5.3
        "behat/behat": "~3.0", // v3.0.15
        "behat/mink-extension": "~2.0.1", // v2.0.1
        "behat/mink-goutte-driver": "~1.1.0", // v1.1.0
        "behat/mink-selenium2-driver": "~1.2.0", // v1.2.0
        "phpunit/phpunit": "~4.6.0" // 4.6.4
    }
}

In course 1, we added our basic endpoints with some nice tests (yes!).

Now, we turn to a huge part of APIs: handling and returning consistent errors. Seriously, this is more important than you might think - which is why we're covering this in episode 2.

So, let's handle errors, and let's do it gracefully:

  • Handling Form Validation errors and response
  • Disabling csrf_protection
  • Introducing the api-problem JSON format
  • Exception Listener!
  • Consistent errors everywhere: 404's, 500's, oh my!
  • Handling "extra fields" in your form

Next courses in the APIs: REST in Symfony 2 & 3 section of the APIs Track!

15 Comments

Sort By
Login or Register to join the conversation
Default user avatar Hugo do Carmo 6 years ago edited

Just a reminder, if you pretend to use Guzzle 6, it now throws exception for 400 and 500 level of errors (see https://stackoverflow.com/a/30957410/2214160 )

    /**
     * @return GuzzleHttp\Psr7\Response
     */
    protected function request($uri, array $options, $method = 'GET')
    {
        $response = new Response();

        try {
            $response = $this->client->request($method, $uri, $options);
        } catch(ClientException $e) { /* 400 errors */
            $response = $e->getResponse();
        } catch(ServerException $e) { /* 500 errors */
            $response = $e->getResponse();
        }

        return $response;
    }
1 | Reply |

Hey Hugo do Carmo

We don't use Guzzle for this specific course, but thanks for the info anyways!

Have a nice day

| Reply |
Default user avatar Hugo do Carmo MolloKhan 6 years ago edited

Hi MolloKhan, although it's not declared as a dependency, the Guzzle client is used to test the programmer controller, as you can see in ApiTestCase, ResponseAsserter and ProgrammerControllerTest.

Since it's not declared as a dependency, the developer have the freedom to choose the most recent version, which is incompatible with the current code in this case.

Cheers.

| Reply |

Ohh, you are totally right Hugo, sorry for misleading you. If you want to use Guzzle 6 for this course I believe it won't be that hard to tweak the code so it works properly

Have a nice day

| Reply |
Default user avatar Rosemary 4 years ago

Will you be updating this course for Symfony 5?

| Reply |

Hey Rosemary!

No this course specifically, unfortunately. For Symfony 5, we'll likely be focusing more on API Platform - https://symfonycasts.com/sc... - because it's just SO useful for handling SO many things for you. However, I also want people to be able to learn the fundamentals and create API endpoints without API Platform. Have you tried API Platform? If yes (or no), what are the things that you are especially looking for in this tutorial that you would like to see in Symfony 5? Feedback always motivates what we do :).

Cheers!

| Reply |

getErrorsFromForm

| Reply |

Returns the following:

{

"type": "validation_error",

"title": "There was a validation error",

"errors": [

"The CSRF token is invalid. Please try to resubmit the form.",

"Please enter a clever nickname"

]

}

without the 'nickname' subelement.

| Reply |

Sorry, disregard! My fault. Please delete.

| Reply |

No worries Vlad :)

| Reply |
Default user avatar Danny 6 years ago

Hey guys,

When can be expect this to start? :)

| Reply |

Yo Danny! With any luck, we'll start releasing things next week - and we'll finish by the end of June :).

Cheers!

1 | Reply |

Nice. Looking forward to it!

| Reply |
Default user avatar Simone Biffi 6 years ago

Hey guys,
but this course will not keep more?
Thank's

| Reply |

We definitely are - we'll have more this week and yet more next week :)

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!