API environment contextual responses with Symfony3 Subscribers for Dev vs. Non-Dev

According to the ideal 12 Factor App you want to store your configuration in the specific environment. This includes debugging, logging, and everything else your application does. When building an API, these days you want your response data to be JSON, unless you’re an XML holdout which case you’re still using XML. Regardless, while in production you want clear and concise error message responses that do not reveal too much information. However, outside of production you probably want a quick way to see at least the source file throwing the exception so you know where to begin without crawling logs for every iterative issue while in development. For this you’ll need your API response data to include some contextual debugging and/or exception information you normally wouldn’t want in your response. This is a perfect scenario for environment configuration, and Symfony has a great way we can handle this with an ExceptionSubscriber for “Dev” and for “Non-Dev” environments (in this example).

Here’s a brief breakdown of what we’re building:

  1. Service declaration for subscribing to exceptions in dev and default environments
  2. ExceptionSubscriber class that processes exceptions being thrown, and cleans up the response JSON (…and XML because we aren’t jerks).
  3. DevExceptionSubscriber that extends ExceptionSubscriber and adds some debug “stuff” to the response.

Declare the Default Service

Build ExceptionSubscriber

 

Extend ExceptionSubscriber for DevExceptionSubscriber

Here, we don’t filter any of the message content, so we can get the literal exception text thrown for exception classes we didn’t specifically write. This is particularly useful if we want to mask exceptions for invalid passwords so users cannot guess which usernames exist or not, and other business rule exceptions for Unauthorized or Access Denied messages where we don’t want to reveal too much.

Finally..

In use, you’ll know see responses like this:

Throw Exception based on Accept Header

Symfony has a wonderful onKernelException event that we can expose in an Event Listener to properly handle exceptions in our applications.

In one particular use case, we want to handle JSON and XML responses for our exceptions that are thrown via our API. In this case, we need to format and render our exception based on the Accept Header of the initial request.

This is handled in two simple steps:

  1. An ApiExceptionListener class that processes the onKernelException event.
  2. A service declaration that tells the Symfony kernel about our Event Listener.

The Exception Listener

We need to be careful above not to check the Accept header for XML because some browsers will give a false positive on this. As such, we check instead for an AJAX request that was not JSON, which thus must be XML.

Defining the service

Sample Response

Now that we’ve got this setup, our exception messages are nice and clean: