A modern error handler capable of logging and formatting errors in a variety of ways.

Related tags

API booboo
Overview

BooBoo

Quality

Build Status Code Climate Test Coverage

Purpose

An error handler for PHP that allows for the execution of handlers and formatters for viewing and managing errors in development and production. Because we all make mistakes.

Installation

This library requires PHP 5.4 or later. One test is skipped against HHVM, but all others pass. BooBoo will support PHP 7 in a future release.

It is recommended that you install this library using Composer.

$ composer require league/booboo

BooBoo is compliant with PSR-1, PSR-2, PSR-3 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Dependencies

BooBoo relies upon and requires the following dependencies:

  • psr/log - A PSR-3 compliant interface for logging.

No other dependencies are required. The maintainer recommends installing monolog for logging.

Advantages Over Existing Solutions

BooBoo is designed to help make development easier while providing an integrated solution that can be deployed to your production environment. BooBoo offers the following advantages:

Errors are non-blocking by default

Some solutions throw exceptions for all errors, causing every notice to become a fatal error. BooBoo doesn't do this. Rather than raise an exception for non-fatal errors, we display the error to you in a way that makes sense and lets the program continue running. An E_NOTICE shouldn't become an exception.

The Runner object offers a method for forcing all errors to be blocking, should you wish to throw exceptions for more minor errors. This is turned off by default.

BooBoo won't end up in your stack trace

Because we don't throw an exception by default, we don't generate a stack trace for minor errors. This means BooBoo won't show up in your logs, and when it handles an exception generated elsewhere, it isn't appended there, either.

BooBoo is built for logging

This solution is designed with logging in mind, so that you can plug and play a PSR-3 compliant logging solution in and go. BooBoo is sensitive enough to log errors, warnings and notices as such; exceptions are logged as critical, and E_STRICT/E_DEPRECATED warnings are logged as info. Handlers run even if formatting is disabled, so your logging will always be on, even in production.

BooBoo is designed for extension

We can't possibly think through all your use cases, but we know you can. That's why we use standard interfaces to make it easy to extend and enhance the BooBoo functionality. (We also love pull requests; please share your innovations!)

BooBoo is actively maintained

PHP is changing every year, and BooBoo will change along with it.

Getting Started

Instantiation

The main object that you need to instantiate is League\BooBoo\BooBoo. This object takes care of setting the error handler, as well as handling errors and exceptions. It takes optional arguments during construction for handlers and formatters.



$booboo = new League\BooBoo\BooBoo();
$booboo->register(); // Registers the handlers

It's very important to call BooBoo::register() or the object won't register itself as PHP's error handler.

Formatters are very important!

When you're developing, you want to view errors in the browser. In order to do this, you must provide a formatter. Without a formatter, the system won't intelligently know how to display the errors. As a result, the Runner will throw an exception and won't register the error handlers.

The library ships with four formatters for your convenience:

  • HtmlFormatter - Formats errors just like PHP's error formatting.
  • HtmlTableFormatter - Formats errors and exceptions similar to Xdebug, wth a full stack trace, even for errors.
  • JsonFormatter - Perfect for displaying errors to an API.
  • CommandLineFormatter - Working with the command line? This will produce pretty command-line errors.
  • NullFormatter - This formatter simply silences all errors. You can pass this when display_errors = Off. Adding a formatter is easy:


$booboo->pushFormatter(new League\BooBoo\Formatter\HtmlFormatter());

Controlling which formatter does the formatting

There may be times that you want certain formatters to handle the formatting for particular errors, and others to handle the formatting for other error types. Formatters support this.

For example, if you want all errors of warning or higher to show in the browser, but errors that are below this level to be ignored, you can configure the formatters to handle this scenario as such:



$html = new League\BooBoo\Formatter\HtmlFormatter();
$null = new League\BooBoo\Formatter\NullFormatter();

$html->setErrorLimit(E_ERROR | E_WARNING | E_USER_ERROR | E_USER_WARNING);
$null->setErrorLimit(E_ALL);

$booboo->pushFormatter($null);
$booboo->pushFormatter($html);

Formatters and handlers are a stack

Formatters and handlers are treated as a stack. This means that the last item in will be the first item out. This is very important when dealing with formatters that only handle certain errors!

For example, in the example above, we have one formatter limited to errors and warnings, and the other formatting all error types. If we insert the HTML handler first, it will be run last; this would cause the NullFormatter to format all errors, and we would get no output.

Handlers

Regardless of whether or not you want to format the error (or even output it to the screen), you may want to handle it in some way, such as logging it. BooBoo provides a way to handle errors, and provides a built-in PSR-3 compatible logging handler.

You can implement the HandlerInterface to create your own handlers. Handlers are run regardless of whether or not display_errors is true. Unlike formatters, you cannot direct handlers to ignore certain errors; it's assumed that you can handle this with the services that handlers work through.

Documentation

Check out the documentation here

Security

If you have identified a security issue, please email [email protected] directly. Do not file an issue as they are public!

Comments
  • Pretty page formatter

    Pretty page formatter

    I am not sure what the plans are, but this package could borrow the pretty page handler from whoops which is awesome (at least tiny).

    My favourite feature is the open in editor, which makes it super easy to debug the application and solve problems fast.

    opened by sagikazarmark 34
  • Could we please have a tagged release for b43a29b6

    Could we please have a tagged release for b43a29b6

    Firstly, thank you for this project. BooBoo has been a big help in the projects I work on.

    I was using release 1.0.0 but based on some of the more recent activity, I started using the master branch ( which is always a risky idea 😀 ).

    With 521257a a break change was introduced to the master branch.

    Would you please tag a release (e.g. 1.1.0) for b43a29b6 (i.e. just before the recent breaking changes)?

    I can pin the requirement to the specific commit, which I'm doing right now, but I'd love to be able to target a version instead.

    Thanks again for all you put into this project and for allowing us to benefit from it.

    opened by salcode 4
  • CLI not working?

    CLI not working?

    Pretty straight forward implementation, I have BooBoo being brought in via composer, I push the formatter and register the instance. I kick off an infinite loop and immediately throw an exception. In my terminal I run php index.php and the process immediately exits, I am not presented with any visual output. Am I missing something?

    <?php
    
    // Autoload packages
    require __DIR__ . '/vendor/autoload.php';
    
    $runner = new League\BooBoo\Runner();
    $runner->pushFormatter(new \League\BooBoo\Formatter\CommandLineFormatter());
    $runner->register();
    
    // Kick off infinite loop
    while(true) {
        throw new Exception('a');
    }
    
    
    opened by HeathNaylor 2
  • exceptionHandler doesn't return 500 status when displaying errors

    exceptionHandler doesn't return 500 status when displaying errors

    Is it by design to mimic how PHP behaves or can I submit a pull request to add the 500 status header when display_errors is set to true?

    My main problem with the actual implementation is when I make Ajax request I get a 200 status header even though the request crashed.

    If it's by design, should I make my own formatter to add the status header, or is there a better way?

    opened by nebulousGirl 2
  • Showing exception codes in formatters ?

    Showing exception codes in formatters ?

    There are occasions where the same exception class is used in different occasions with a different "code" argument (e.g. Exception::getCode()). It will be nice if this is included when rendering with the formatters when this code is different than zero (its default value). I understand that the backtrace can guide you very easily to where the exception was thrown, but there are also occasions (such as when logging the errors/exception) that it will be easier just to be able to read the exception code along with exception message

    enhancement 
    opened by kktsvetkov 2
  • Error-formatters auto-disabled in production?

    Error-formatters auto-disabled in production?

    According to php docs, the display_errors settings "is a feature to support your development and should never be used on production systems".

    Because BooBoo conditionally bypasses all formatters, effectively this means error-formatters can only be used on development-systems, not on production-systems.

    Is this "by design"?

    I ask because we were expecting we'd be able to create a user-facing error formatter for use in production, e.g. something that doesn't reveal any technical details, but at least explains that there was an error, sends an appropriate 500 status code, etc. - it sounds like error-formatters can't be used for that?

    Of course, error-handlers can be used in production, but that's not really "enough", is it? The blank 200 OK page for errors is very misleading and highly undesirable, both in production and development - the blank page is a huge, classical php wtf for developers and end-users alike, I thought BooBoo was supposed to help remedy that?

    I could implement my production error "formatter" as an error handler and send output from it, but that sounds really wrong - is that the only way to use BooBoo in production? (short of turning on display_errors in production, which "should never be used on production systems" - and which could reveal sensitive information if BooBoo fails to trap a fatal error...)

    opened by mindplay-dk 2
  • chore: update travis-ci file for maintained php versions

    chore: update travis-ci file for maintained php versions

    This request is to update the versions in the Travis-CI file for the supported PHP versions as of July 2021:

    • 7.3 (maintenance until December 2021)
    • 7.4 (active or maintenance until November 2022)
    • 8.0 (active or maintenance until November 2023)
    opened by chrisforrence 1
  • Inquiry: BooBoo compared to Log4PHP

    Inquiry: BooBoo compared to Log4PHP

    So I'm curious about the differences BooBoo has compared to Log4PHP. What does it bring to the table? Would you say one is better than the other or more about how you want to log something is the difference?

    I'm just curious.

    opened by KnightYoshi 1
  • Argument 1 passed to exceptionHandler() must be an instance of Exception, instance of Error given

    Argument 1 passed to exceptionHandler() must be an instance of Exception, instance of Error given

    composer.json

        "require": {
            "league/booboo": "^1.0"
        },
    

    php -v output

    PHP 7.1.2-2+deb.sury.org~yakkety+1 (cli) (built: Feb 18 2017 11:18:02) ( NTS )
    Copyright (c) 1997-2017 The PHP Group
    Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
        with Zend OPcache v7.1.2-2+deb.sury.org~yakkety+1, Copyright (c) 1999-2017, by Zend Technologies
    
    

    index.php

    <?php
    
    require __DIR__ . '/../vendor/autoload.php';
    
    $errorHandler = new League\BooBoo\Runner([new League\BooBoo\Formatter\HtmlFormatter]);
    $errorHandler->register();
    
    new ASDWQEASD();//not an existing class
    

    Expected result: error handling Actual result: I get an error "Argument 1 passed to League\BooBoo\Runner::exceptionHandler() must be an instance of Exception, instance of Error given" in my error log

    opened by DemoniacDeath 1
  • Expose formatter content type

    Expose formatter content type

    Having each formatter declare their content type makes it easier to:

    1. Choose the right formatter based on content negotiation.
    2. Output the correct content type when rendering.

    Being able to include multiple formatters that handle the same type of error allows for content negotiation via HTTP request headers. This is advantageous in applications that return multiple content types, such as APIs that may return either JSON or XML.

    Fixes #21

    opened by shadowhand 1
  • Wrong HTTP status code on error

    Wrong HTTP status code on error

    The HTTP status code is only set when using an error page: https://github.com/thephpleague/booboo/blob/master/src/Runner.php#L135

    Is this the desired behavior?

    opened by nebulousGirl 1
  • Help !

    Help !

    Problem 1 - Root composer.json requires league/booboo ^2.1 -> satisfiable by league/booboo[2.1]. - league/booboo 2.1 requires psr/log ~1.0 -> found psr/log[1.0.0, ..., 1.1.4] but the package is fixed to 3.0.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.

    opened by MsZhu 0
  • Fix deprecated warnings for Interfaces in FrameCollection class

    Fix deprecated warnings for Interfaces in FrameCollection class

    Problem

    There are 7 deprecation warnings presented by the package when composer is first run, and also when the HtmlFormatter are used with E_NOTICE turned on.

    These warning prevent composer from installing/updating packages in development environments with strict levels of error reporting/handling

    Solution

    These are all fixed by simply adding the return types for the interface methods (e.g. ArrayAccess::offsetSet(): void)

    Log output of errors

    Deprecated: Return type of League\BooBoo\Util\FrameCollection::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 64
    
    Deprecated: Return type of League\BooBoo\Util\FrameCollection::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 73
    
    Deprecated: Return type of League\BooBoo\Util\FrameCollection::offsetSet($offset, $value) should either be 
    compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] 
    attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 82
    
    Deprecated: Return type of League\BooBoo\Util\FrameCollection::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 91
    
    Deprecated: Return type of League\BooBoo\Util\FrameCollection::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 
    55
    
    Deprecated: Return type of League\BooBoo\Util\FrameCollection::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in C:\laragon\www\thephpleague\booboo\src\Util\FrameCollection.php on line 100
    
    Deprecated: League\BooBoo\Util\Frame implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in C:\laragon\www\thephpleague\booboo\src\Util\Frame.php on line 12
    

    Thanks

    opened by lukewatts 1
  • Update the docs

    Update the docs

    Please, the docs are confusing to init. I think that added some code lines will be more clearly to start with this module. I'm assuming that use is defined on head of php file to work with namespaces:

    • Add a empty array to BooBoo constructor prevent: Uncaught ArgumentCountError (Too few arguments to function League\BooBoo\BooBoo::__construct()).
    • Get an order to work with BooBoo.
    • Understand the workflow from this application.
    // first
    $booboo = new BooBoo([]);
    
    // set formatters before call register
    $html = new HtmlFormatter();
    $null = new NullFormatter();
    
    $html->setErrorLimit(E_ERROR | E_WARNING | E_USER_ERROR | E_USER_WARNING);
    $null->setErrorLimit(E_ALL);
    
    $booboo->pushFormatter($null);
    $booboo->pushFormatter($html);
    
    // finally, call to register to run BooBoo
    $booboo->register();
    

    The main error can be resolved added a default empty array to BooBoo constructor too if you want to add any code modification.

    Thanks.

    opened by ghost 1
  • Adding deep CLI formatter

    Adding deep CLI formatter

    Many times, only knowing the most recent exception is not enough. I've created a deep formatter for CLI uses that shows the previous exceptions on the stack, and has a simplified text.

    selection_014

    opened by igorsantos07 2
  • Formatters should include a Content-Type

    Formatters should include a Content-Type

    Formatters should also do something like this:

    // JsonFormatter
    public function getContentType()
    {
        return 'application/json';
    }
    

    So that when the formatted output is returned, the appropriate type can be set:

    // Runner
    list ($mime, $body) = $this->runFormatters($e);
    header('Content-Type: ' . $mime);
    echo $body;
    
    opened by shadowhand 6
Owner
The League of Extraordinary Packages
A group of developers who have banded together to build solid, well tested PHP packages using modern coding standards.
The League of Extraordinary Packages
A lightweight middleware to make api routing session capable.

Laravel stateless session A lightweight middleware to make api routing session capable. Installing $ composer require overtrue/laravel-stateless-sessi

安正超 17 Jul 6, 2022
Modern version of pocketmine forms API, ported to PHP 8.0+ with high quality code and phpstan integration

forms Modern version of pocketmine forms API, ported to PHP 8.0+ with high quality code and phpstan integration Code samples ModalForm Using ModalForm

Frago9876543210 23 Nov 18, 2022
💛 Modern API development in Laravel. ✍️ Developed by Gentrit Abazi.

Introduction Larapi is a package thats offers you to do modern API development in Laravel with support for new versions of Laravel. Larapi comes inclu

one2tek 93 Oct 28, 2022
A RESTful and extendable Backend as a Service that provides instant backend to develop sites and apps faster, with dead-simple integration for JavaScript, iOS, Android and more.

Welcome to hook ![Gitter](https://badges.gitter.im/Join Chat.svg) hook is a RESTful, extendable Backend as a Service that provides instant backend to

doubleleft 762 Dec 30, 2022
Online Book Store is a E-commerce Website and Book Conversion(pdf to audio and Img to txt) and Book Sharing platform.

Online-Book-Store Online Book Store is a E-commerce Website and Book Conversion(pdf to audio and Img to txt) and Book Sharing platform. The main descr

Gokul krishnan 1 May 22, 2022
Read and write OpenAPI 3.0.x YAML and JSON files and make the content accessible in PHP objects.

php-openapi Read and write OpenAPI 3.0.x YAML and JSON files and make the content accessible in PHP objects. It also provides a CLI tool for validatin

Carsten Brandt 399 Dec 23, 2022
Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and / or RESTful API

Luracast Restler ![Gitter](https://badges.gitter.im/Join Chat.svg) Version 3.0 Release Candidate 5 Restler is a simple and effective multi-format Web

Luracast 1.4k Dec 14, 2022
A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

Woohoo Labs. 153 Sep 5, 2022
A bundle providing routes and glue code between Symfony and a WOPI connector.

WOPI Bundle A Symfony bundle to facilitate the implementation of the WOPI endpoints and protocol. Description The Web Application Open Platform Interf

Champs-Libres 5 Aug 20, 2022
This API provides functionality for creating and maintaining users to control a simple To-Do-List application. The following shows the API structure for users and tasks resources.

PHP API TO-DO-LIST v.2.0 This API aims to present a brief to consume a API resources, mainly for students in the early years of Computer Science cours

Edson M. de Souza 6 Oct 13, 2022
a tool to get Facebook data, and some Facebook bots, and extra tools found on Facebook Toolkit ++.

FACEBOOK TOOLKIT a tool to get Facebook data, and some Facebook bots, and extra tools found on Facebook Toolkit ++. Graph API Facebook. Made with ❤️ b

Wahyu Arif Purnomo 569 Dec 27, 2022
Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.

API Platform is a next-generation web framework designed to easily create API-first projects without compromising extensibility and flexibility: Desig

API Platform 7.7k Jan 7, 2023
Simple utility and class library for generating php classes from a wsdl file.

wsdl2phpgenerator Simple WSDL to PHP classes converter. Takes a WSDL file and outputs class files ready to use. Uses the MIT license. Announcement: We

null 802 Dec 10, 2022
A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.

Slack for PHP | A simple PHP package for sending messages to Slack with incoming webhooks, focused on ease-of-use and elegant syntax. supports: PHP 7.

null 128 Nov 28, 2022
Quickly and easily expose Doctrine entities as REST resource endpoints with the use of simple configuration with annotations, yaml, json or a PHP array.

Drest Dress up doctrine entities and expose them as REST resources This library allows you to quickly annotate your doctrine entities into restful res

Lee Davis 88 Nov 5, 2022
A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of the individual components, but also of the framework.

chubbyphp-framework Description A based PSR-15 microframework that also sets maximum flexibility with minimum complexity and easy replaceability of th

chubbyphp 106 Dec 9, 2022
A RESTful API package for the Laravel and Lumen frameworks.

The Dingo API package is meant to provide you, the developer, with a set of tools to help you easily and quickly build your own API. While the goal of

null 9.3k Jan 7, 2023
An easy to use Fractal wrapper built for Laravel and Lumen applications

An easy to use Fractal wrapper built for Laravel and Lumen applications The package provides a nice and easy wrapper around Fractal for use in your La

Spatie 1.8k Dec 30, 2022
This API aims to present a brief to consume a API resources, mainly for students in the early years of Computer Science courses and the like.

Simple PHP API v.1.0 This API aims to present a brief to consume a API resources, mainly for students in the early years of Computer Science courses a

Edson M. de Souza 14 Nov 18, 2021