Jump to:

3373 Posts in 998 Topics by 712 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: 1125 Views
  • amdayton
    Avatar
    Community Member
    8 Posts

    Error Handling with RestfulService Link to this post

    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

  • MarijnKampf
    Avatar
    Community Member
    161 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);
             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");
    }

    1125 Views
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.