Q:

When error is it good to send different formats?

On a REST api I have these endpoints:
GET: /tracks.json
GET: /tracks.xml
GET: /tracks.csv

It receives a tracks resourse and the format gets defined by .^string^ that is after the resourse. eg the tracks.csv sends the tracks resourse to the client as csv format (with the appropriate mime extention to Content-Type http header).
Sometimes a framework oriented exception gets thrown that gets handled via the following ExceptionListener.
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // You get the exception object from the received event
        $exception = $event->getException();
        $message = array(
            'message'=>$exception->getMessage(),
            'code'=>$exception->getCode(),
            'stacktrace'=>$exception->getTrace()
        );

        // Customize your response object to display the exception details
        $response = new Response();
        $response->setContent(json_encode($message,JSON_PRETTY_PRINT));

        // HttpExceptionInterface is a special type of exception that
        // holds status code and header details
        if ($exception instanceof HttpExceptionInterface) {
            $response->setStatusCode($exception->getStatusCode());
            $response->headers->replace($exception->getHeaders());
        } else {
            $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
            $response->headers->set('content/type','application/json');
        }

        // Send the modified response object to the event
        $event->setResponse($response);
    }
}

So I have the following dilema:
To use http request headers in order to determine the way that response will be shown or just whenever error throw it as json? What would be rest-sane according to that the response should be in a uniform way?

A:

Returning an error in the requested format is perfectly valid, and preferable. This gives clients of your API a standardized way to handle error conditions, and allow your application to communicate the error to the client.
If your API has a default content type, then fall back to that if the HTTP request does not specify the content type. If your application does not have a default content type, then returning JSON is fine too.
Just document the behavior so clients can build in their own error handling as they see fit.

