2017 © Pedro Peláez
 

library chubbyphp-error-handler

Chubbyphp Error Handler

image

chubbyphp/chubbyphp-error-handler

Chubbyphp Error Handler

  • Friday, April 20, 2018
  • by dominikzogg
  • Repository
  • 0 Watchers
  • 0 Stars
  • 442 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

chubbyphp-error-handler

Build Status Total Downloads Latest Stable Version Scrutinizer Code Quality Code Coverage, (*1)

Description

A simple Error Handler Interface for PSR7., (*2)

Requirements

  • php: ~7.0
  • psr/http-message: ~1.0

Suggest

  • pimple/pimple: ~3.0
  • willdurand/negotiation: ~2.1

Installation

Through Composer as [chubbyphp/chubbyphp-error-handler][1]., (*3)

composer require chubbyphp/chubbyphp-error-handler "~1.1"

Usage

SimpleErrorHandler / AdvancedErrorHandler

JsonErrorResponseProvider (implements ErrorHandlerProvider)

<?php

namespace MyProject\ErrorHandler;

use Chubbyphp\ErrorHandler\ErrorHandlerProvider;

class JsonErrorResponseProvider implements ErrorHandlerProvider
{
    /**
     * @return string
     */
    public function getContentType(): string
    {
        return 'application/json';
    }

    /**
     * @param Request $request
     * @param Response $response
     * @param \Exception $exception
     * @return Response
     */
    public function get(Request $request, Response $response, \Exception $exception): Response
    {
        $response->getBody()->write(json_encode([
            'exception' => ['message' => $exception->getMessage(), 'code' => $exception->getCode()]])
        );

        return $response;
    }
}

ErrorHandlerMiddleware

<?php

use Chubbyphp\ErrorHandler\ErrorHandlerMiddleware;
use Chubbyphp\ErrorHandler\ErrorHandlerInterface;

$middleware = new ErrorHandlerMiddleware(new <ErrorHandlerInterface>);
$middleware($request, $response, $next);

SimpleErrorHandler

SimpleErrorHandler

<?php

use Chubbyphp\ErrorHandler\SimpleErrorHandler;

$errorHandler = new SimpleErrorHandler($provider);

$response = $errorHandler($request, $response, $exception);

SimpleErrorHandlerProvider (Pimple)

<?php

use Chubbyphp\ErrorHandler\SimpleErrorHandlerProvider;
use MyProject\ErrorHandler\JsonErrorResponseProvider;
use Pimple/Container;

$container = new Container();
$container->register(new SimpleErrorHandlerProvider);

// IMPORTANT: without this definition, the error handler will not work!
$container['errorHandler.defaultProvider'] = function () use ($container) {
    return new JsonErrorResponseProvider;
};

$app->add($container['errorHandler.middleware']);

AdvancedErrorHandler

ContentTypeResolver (needed only for multi content type error handler)

<?php

use Chubbyphp\ErrorHandler\ContentTypeResolver;
use Negotiation\Negotiator;
use Psr\Http\Message\ServerRequestInterface as Request;

$resolver = new ContentTypeResolver(new Negotiator);
$resolver->getContentType($request, ['text/html']);

AdvancedErrorHandler

<?php

use Chubbyphp\ErrorHandler\AdvancedErrorHandler;

$errorHandler = new AdvancedErrorHandler($resolver, $fallbackProvider, $providers);

$response = $errorHandler($request, $response, $expection);

AdvancedErrorHandlerProvider (Pimple)

<?php

use Chubbyphp\ErrorHandler\AdvancedErrorHandlerProvider;
use MyProject\ErrorHandler\JsonErrorResponseProvider;
use MyProject\ErrorHandler\XmlErrorResponseProvider;
use Pimple/Container;

$container = new Container();
$container->register(new AdvancedErrorHandlerProvider);

// IMPORTANT: without this definition, the error handler will not work!
$container['errorHandler.defaultProvider'] = function () use ($container) {
    return new JsonErrorResponseProvider;
};

// optional: add more than the default provider
$container->extend('errorHandler.providers', function (array $providers) {
    $providers[] = new XmlErrorResponseProvider;

    return $providers;
});

$app->add($container['errorHandler.middleware']);

Dominik Zogg 2016, (*4)

The Versions

20/04 2018

dev-master

9999999-dev

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

05/05 2017

1.1.2

1.1.2.0

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

26/04 2017

1.1.1

1.1.1.0

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

03/11 2016

1.1.0

1.1.0.0

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

03/11 2016

1.1.0-beta1

1.1.0.0-beta1

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

17/10 2016

1.0.0

1.0.0.0

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

12/10 2016

1.0.0-beta1

1.0.0.0-beta1

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp

04/10 2016

1.0.0-alpha1

1.0.0.0-alpha1

Chubbyphp Error Handler

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dominik Zogg

slim error-handler chubbyphp