vtgianni/oops-bundle

A Symfony bundle to log API requests errors.

Installs: 30

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.1 2023-05-17 13:57 UTC

This package is auto-updated.

Last update: 2024-10-10 11:30:45 UTC


README

Logo

About

Oops Bundle is a Symfony bundle allowing you to manage the error returns of your API calls.

Install

Requirements

⚠️ Here are the requirements for using Oops Bundle:

php: >=7.4
doctrine/orm: >=2.13
symfony/framework-bundle: >=5.4
symfony/validator: >=4.4
symfony/http-client: >=4.4

Get started

To get started with Oops Bundle, use Composer to add the package to your project's dependencies:

composer require vtgianni/oops-bundle

Then update your database schema with the following command:

php bin/console d:s:u --force

How to use it

There are two ways to use Oops Bundle: automatic or manual error logging.

Automatic error logging

Oops Bundle provides a wrapper for HttpClient requests.

This allows you to make your API calls without worrying about errors as they will be automatically recorded in your database "oops" table.

Start by injecting OopsClient into your class:

use VTGianni\OopsBundle\Service\OopsClient;

class MyService
{
    private $oopsClient;

    public function __construct(OopsClient $oopsClient)
    {
        $this->oopsClient = $oopsClient;
    }
}

Then make your API calls in the same way as with HttpClient:

$this->oopsClient->request(
    'GET',
    'https://pokeapi.co/api/v2/pokemon/ditto'
);

That's it! If the API returns a status code greater than or equal to 400, it will be automatically recorded in your "oops" table.

Manual error logging

Oops Bundle also gives you direct access to some methods used to interact with the oops table.

This allows you to choose which specific data to record or to manage specific error cases.

Start by injecting OopsService into your class:

use VTGianni\OopsBundle\Service\OopsService;

class MyService
{
    private $oopsService;

    public function __construct(OopsService $oopsService)
    {
        $this->oopsService = $oopsService;
    }
}

Report an incident

To report an incident, please use the reportError method of OopsBundle:

$this->oopsService->reportError(
    $url, // required string
    $statusCode, // required int
    $message, // optional string
    $headers, // optional array
    $bodyContent, // optional array
    $responseContent // optional array
);

Filter errors

To filter errors, please use the filterErrors method of OopsBundle:

$this->oopsService->filterErrors(
    $errorCode, // optional int
    $desc, // optional bool, default true
    $limit // optional int, default 10
);

Count errors

To count errors, please use the countErrors method of OopsBundle:

$this->oopsService->countErrors(
    $nbDays, // optional int, default 7
    $errorCode // opitonal int
);