Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Error Handling with RestfulService

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1196 Views
  • amdayton
    Community Member
    8 Posts

    Error Handling with RestfulService Link to this post


    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,

  • MarijnKampf
    Community Member
    164 Posts

    Re: Error Handling with RestfulService Link to this post

    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);

    And define the following functions in your class:

    function exceptionHandler($exception) {

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

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.