PHP code example of chiron / http-exceptions

1. Go to this page and download the library: Download chiron/http-exceptions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chiron / http-exceptions example snippets


throw new \Chiron\Http\Exception\Client\BadRequestHttpException(); 

$e = new \Chiron\Http\Exception\Client\BadRequestHttpException("Invalid syntax !");
throw $e->setHeaders(['X-Custom-Header' => 'foobar']);

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpException $e) {
    http_response_code($e->getStatusCode());
    header("Content-type: text/html");
    print "<h1>" . $e->getMessage() . "</h1>";
}

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpExceptionInterface $e) {
    $response = $response
        ->withStatus($e->getStatusCode())
        ->withHeader("Content-type", "text/html")
        ->getBody()->write("<h1>" . $e->getMessage() . "</h1>");
}

$e = new \Chiron\Http\Exception\Client\ForbiddenHttpException();

$e->setTitle('You do not have enough credit.');
$e->setDetail('Your current balance is 30, but that costs 50.');
$e->setType('https://example.com/probs/out-of-credit');
$e->setIntance('https://example.net/account/12345/msgs/abc');
$e->setAdditionalData(['balance' => 30, 'accounts' => ['https://example.net/account/12345', 'https://example.net/account/67890']]);

throw $e;

try {
    // ... 
} catch (\Chiron\Http\Exception\HttpException $e) {
    http_response_code($e->getStatusCode());
    header("Content-type: application/problem+json");
    print json_encode($e);
}