2017 © Pedro PelĂĄez
 

symfony-bundle rest-util-bundle

Bundle for integrating tbbc/rest-util lib in a Symfony application.

image

tbbc/rest-util-bundle

Bundle for integrating tbbc/rest-util lib in a Symfony application.

  • Tuesday, June 16, 2015
  • by tbbc
  • Repository
  • 8 Watchers
  • 32 Stars
  • 45,255 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 8 % Grown

The README.md

The Big Brains Company - TbbcRestUtilBundle

Build Status Scrutinizer Quality Score, (*1)

A bundle for integrating tbbc/rest-util lib in a Symfony application, (*2)

Table of contents

  1. Installation
  2. Quick start
  3. Usage
  4. Run the test
  5. Contributing
  6. Requirements
  7. Authors
  8. License

Installation

Using Composer, just $ composer require tbbc/rest-util-bundle package or:, (*3)

{
  "require": {
    "tbbc/rest-util-bundle": "@stable"
  }
}

Quick start

Handling errors in a REST(ful) API

 Configuration

tbbc_rest_util:
    error:
        use_bundled_factories: true
        exception_mapping:
            FormErrorException:
                class: "Tbbc\\RestUtilBundle\\Error\\Exception\\FormErrorException"
                factory: tbbc_rest_util_form_error
                http_status_code: 400
                error_code: 400101
                error_message: "Invalid input"
                error_more_info_url: "http://api.my.tld/doc/error/400101"
            AccessDeniedException:
                class: "Symfony\\Component\\Security\\Core\\AccessDeniedException"
                factory: default
                http_status_code: 401
                error_code: 401001
                error_message: "Access denied"
                extended_message: "The given token don't have enough privileges for accessing to this resource"
                error_more_info_url: "http://api.my.tld/doc/error/401001"
            CustomException:
                class: "My\\ApiBundle\\Exception\\CustomException"
                factory: my_api_custom
                http_status_code: 501
                error_code: 501001
                error_more_info_url: "http://api.my.tld/doc/error/501001"
            Exception:
                class: "\\Exception"
                factory: default
                http_status_code: 500
                error_code: 501203
                error_message: "Server error"

Custom Symfony Exception Listener

errorResolver = $errorResolver;
        parent::__construct($controller, $logger);
    }

    /**
     * @param GetResponseForExceptionEvent $event
     * @return void
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        static $handling;

        if (true === $handling) {
            return;
        }

        $exception = $event->getException();
        $error = $this->errorResolver->resolve($exception);
        if (null == $error) {
            return;
        }

        $handling = true;

        $response = new Response(json_encode($error->toArray()), $error->getHttpStatusCode(), array(
            'Content-Type' => 'application/json'
        ));

        $event->setResponse($response);
    }

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::EXCEPTION => array('onKernelException', 10),
        );
    }
}
```

```xml


<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="my_api.event_listener.rest_exception.class">My\ApiBundle\EventListener\RestExceptionListener</parameter>
    </parameters>

    <services>
        <service id="my_api.event_listener.rest_exception" class="%my_api.event_listener.rest_exception.class%">
            <tag name="kernel.event_subscriber" />
            <tag name="monolog.logger" channel="request" />
            <argument type="service" id="tbbc_rest_util.error.error_resolver" />
            <argument>%twig.exception_listener.controller%</argument>
            <argument type="service" id="logger" on-invalid="null" />
        </service>
    </services>
</container>

Api Controller code

get('security.context')->isGranted('COMMENT', $post)) {
            throw new AccessDeniedException('Access denied');
        }

        $commentResource = new CommentResource();
        $form = $this->createNamedForm('', new CommentResourceType(), $commentResource);
        $form->bind($this->getRequest());

        if (!$form->isValid()) {
            throw new FormErrorException($form);
        }

        // another error
        if (....) {
            throw new CustomException('Something bad just happened!');
        }

        // ... save comment or whatever
    }
}
```

#### MyApiCustomException error factory

```php
supportsException($exception)) {
            return null;
        }

        $errorMessage = $mapping->getErrorMessage();
        if (empty($errorMessage)) {
            $errorMessage = $exception->getMessage();
        }

        $extendedMessage = $exception->getMoreExtendedMessage();
        // Or whatever you need to do with your exception here

        return new Error($mapping->getHttpStatusCode(), $mapping->getErrorCode(), $errorMessage,
            $extendedMessage, $mapping->getErrorMoreInfoUrl());
    }

    /**
     * {@inheritDoc}
     */
    public function supportsException(\Exception $exception)
    {
        return $exception instanceof CustomException;
    }
}
```

```xml


<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <parameters>
        <parameter key="my_api.error.custom_error_factory.class">My\ApiBundle\Error\Factory\CustomErrorFactory</parameter>
    </parameters>

    <services>
        <service id="my_api.error.custom_error_factory" class="%my_api.error.custom_error_factory.class%">
            <tag name="tbbc_rest_util.error_factory" />
        </service>
    </services>
</container>

ENJOY!, (*4)

For the exceptions thrown in the previous PostCommentsController class example, the response body will be respectively something like the following:, (*5)

For the AccessDeniedException exception:, (*6)

{
    "http_status_code": 401,
    "code": 401001,
    "message": "Access denied",
    "extended_message": "The given token don't have enough privileges for accessing to this resource",
    "more_info_url": "http:\/\/api.my.tld\/doc\/error\/401001"
}

For the FormErrorException exception:, (*7)

{
    "http_status_code": 400,
    "code": 400101,
    "message": "Invalid input",
    "extended_message": {
        "global_errors": [
            "Bubbled form error!"
        ],
        "property_errors": {
            "content": [
                "The comment content should not be blank",
            ]
        }
    },
    "more_info_url": "http:\/\/api.my.tld\/doc\/error\/400101"
}

For the CustomException exception:, (*8)

{
    "http_status_code": 501,
    "code": 501001,
    "message": "Something bad just happened!",
    "extended_message": null,
    "more_info_url": "http:\/\/api.my.tld\/doc\/error\/501001"
}

Usage

Run the test

First make sure you have installed all the dependencies, run:, (*9)

$ composer install --dev, (*10)

then, run the test from within the root directory:, (*11)

$ vendor/bin/phpunit, (*12)

Contributing

  1. Take a look at the list of issues.
  2. Fork
  3. Write a test (for either new feature or bug)
  4. Make a PR

Requirements

  • PHP 5.3+

Authors

  • Benjamin Dulau - benjamin.dulau@gmail.com
  • Valentin Ferriere - valentin@v-labs.fr

License

The Big Brains Company - TbbcRestUtilBundle is licensed under the MIT License - see the LICENSE file for details, (*13)

The Big Brains Company - Logo, (*14)

The Versions

16/06 2015

dev-master

9999999-dev

Bundle for integrating tbbc/rest-util lib in a Symfony application.

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest restful rest-util

29/05 2015

v1.0.2

1.0.2.0

Bundle for integrating tbbc/rest-util lib in a Symfony application.

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest restful rest-util

03/02 2015

v1.0.1

1.0.1.0

Bundle for integrating tbbc/rest-util lib in a Symfony application.

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest restful rest-util

25/11 2014

v1.0.0

1.0.0.0

Bundle for integrating tbbc/rest-util lib in a Symfony application.

  Sources   Download

MIT

The Requires

 

The Development Requires

api rest restful rest-util