Skip to content

Instantly share code, notes, and snippets.

@alganet
Created September 17, 2012 02:52
Show Gist options
  • Save alganet/3735309 to your computer and use it in GitHub Desktop.
Save alganet/3735309 to your computer and use it in GitHub Desktop.
Exception Handlers on Respect\Rest

Exception Handlers on Respect\Rest

Hi! This is the first gist-post about a new feature on one of Respect components.

Let's say you're crazy and designed a Hello World API that throws errors randomly. Definitely most APIs could be classified as such:

<?php
$router = new Respect\Rest\Router;
$router->any('/**', function() {
    if (rand(0,1)) {
        return 'Hello World';
    } else {
        throw new RuntimeException('Gotcha!');
    }
});

Now you can catch those errors! Courtersy of Exception Routes:

<?php
$router->exceptionRoute('RuntimeException', function($e) {
    return 'Caught a Gotcha!';
});

Hope you enjoy that. Also, there is something more:

Error Routes

These one is for catching PHP errors (Notices, Warnings, User Errors):

<?php
$router->errorRoute(function($err) {
    return 'This errors happened: '.var_dump($err);
});

Hope you like it! =D Available now on PEAR and Composer. http://github.com/Respect/Rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment