2017 © Pedro Peláez
 

library json-exception-formatter

JSON for your Exceptions

image

radweb/json-exception-formatter

JSON for your Exceptions

  • Friday, August 15, 2014
  • by danharper
  • Repository
  • 8 Watchers
  • 8 Stars
  • 2,078 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

, (*1)

Build Status Latest Stable Version License, (*2)

Laravel JSON Exception Formatter

A small Laravel package to format & output exceptions in JSON format when required., (*3)

By default in Laravel, throwing an Exception in debug mode will display a nice JSON response when required (eg. an AJAX response, or an Accept: application/javascript header)., (*4)

However once you're not in debug mode (ie. a production environment), a whole HTML response is displayed instead., (*5)

With this package, when you're not in debug mode, exceptions will be output as JSON (only without debug information like the file name & line number)., (*6)

NOTE This does NOT affect HTML requests. Only AJAX/JSON requests are altered., (*7)

Installation

Add radweb/json-exception-formatter to your composer.json file., (*8)

{
    "require": {
        "radweb/json-exception-formatter": "dev-master"
    }
}

In app/config/app.php, add the Service Provider to the providers array:, (*9)

array(
    'providers' => array(
        // ...
        'Radweb\JsonExceptionFormatter\JsonExceptionFormatterServiceProvider',
    )
)

Custom Formatters

You can override the default JSON exception formatter to use a different format, or provide more detail in the output., (*10)

To override, implement the Radweb\JsonExceptionFormatter\FormatterInterface interface, and bind with the IoC container. This requires you to implement two methods: formatDebug() and formatPlain()., (*11)

Example implementation:, (*12)

<?php

use Radweb\JsonExceptionFormatter\FormatterInterface;

class CustomDebugFormatter implements FormatterInterface {

    public function formatDebug(Exception $e)
    {
        return array(
            'theError' => array(
                'message' => $e->getMessage(),
                'detail' => 'In file '.$e->getFile().' on line '.$e->getLine(),
            ),
        );
    }

    public function formatPlain(Exception $e)
    {
        return array(
            'theError' => array(
                'message' => $e->getMessage(),
                // we don't want to display debug details in production
            ),
        );
    }

}

Now we just have to bind it in the IoC container. Add this anywhere in your app's bootstrap code (if you have nowhere, routes.php will do):, (*13)

App::bind('Radweb\JsonExceptionFormatter\FormatterInterface', 'CustomDebugFormatter');

Preview

Normal Request, Debug Mode ENABLED, (*14)

, (*15)

Normal Request, Debug Mode DISABLED, (*16)

, (*17)

JSON Request, Debug Mode ENABLED, (*18)

, (*19)

JSON Request, Debug Mode DISABLED, (*20)

, (*21)

The Versions

15/08 2014

dev-master

9999999-dev

JSON for your Exceptions

  Sources   Download

MIT

The Requires

 

The Development Requires

15/08 2014

0.2.0

0.2.0.0

JSON for your Exceptions

  Sources   Download

MIT

The Requires

 

The Development Requires

13/02 2014

0.1.0

0.1.0.0

JSON for your Exceptions

  Sources   Download

MIT

The Requires

 

The Development Requires