Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Error Handling with RestfulService


Reply


2 Posts   1315 Views

Avatar
amdayton

Community Member, 8 Posts

10 July 2010 at 3:46am

Hello—

This is something that's really been bugging me for a while. Has anyone come up with a graceful solution for error handling with RestfulService? Here's a general scenario I'm talking about:

1. User visits a page that includes content (Twitter, Flickr, RSS feed, whatever) gathered from a web service using RestfulService.
2. When the server sends a request the web service, there is some sort of error (example: Twitter is down) and the response is empty or unusable.
3. The page shows a friendly message "We're having trouble communicating ... try refreshing your browser" or something like that.

For me, the most intuitive way to handle this process would be via error catching, but unfortunately SS doesn't seem to have support for that. A few options that I have considered, none of which have been very satisfactory:

- Chaining variables/functions through the stack that essentially pass the error as data (this gets ugly)
- Assigning some sort of global variable that is flagged when there is a communication error with the web service.

I am wondering if there's something that would integrate well with the SS stack that I might no know about (as I'm still getting the hang of this). Or maybe a workaround? Thoughts?

Thanks for your help,
Andy

Avatar
MarijnKampf

Community Member, 164 Posts

21 July 2010 at 12:27am

Ran into the same problem today. It's a big disappointment that the try catch solution doesn't work in SilverStripe.

However, I did find a way to catch the error yourself by creating error handling functions.

         set_error_handler(array(&$this, 'errorHandler'), error_reporting());
         set_exception_handler(array(&$this, 'exceptionHandler'));
            $fileContent = file_get_contents($url);
         restore_exception_handler();
         restore_error_handler();

And define the following functions in your class:

function exceptionHandler($exception) {
   Debug::Show($exception);
}

function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) {
   Debug::Show("$errno, $errstr, $errfile, $errline, $errcontext");
}