Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
cannot catch exception when running task with oil
  • Hoping I'm just doing something wrong or there's an easy fix for this! I've got a task called coords which retrieves lat/lng coordinates for addresses and saves them in my db. Ultimately the task will be run through cron every 5 minutes to fill in any blank lat/lng fields as users create accounts and enter their addresses. The coords task makes use of a Model_Maps, which has a function called retrieve_lat_lng. In that function, an exception is thrown if the response status from the google maps api is not OK. That's the background. The problem is that when I run the coords task using oil and an exception is throw in retrieve_lat_lng, I get an error output to the command line and execution stops completely. I'm calling retrieve_lat_lng in a proper try/catch block, but I am unable to catch it because execution stops at the "throw new Exception" line in the maps model. When I copy the code from the coords task into a function in a controller and run it through a browser, it works perfectly. Is there a better way to handle exceptions when using oil? I need my maps model to work with both oil/tasks as well as my models and controllers. Thanks for any help! Much appreciated!
  • I did a little digging around in the oil package and found the following commented out in oil/classes/exception :
    // public function __toString()
    // {
    //   \Cli::write('Error: ' . $this->message);
    // }
    

    Curious! I uncommented it for kicks, but it didn't make a difference in the behavior I'm seeing. The command line error I'm getting is formatted something like this: Error - $this->message in FILE on LINE So I tried grepping through the oil package and the core to find a line that does something like that, but no luck... Any help is much appreciated! Thanks! :)
  • Just made a test task:
    <?php
    
    namespace Fuel\Tasks;
    
    class Test
    {
     public static function run()
     {
      try
      {
       throw new \Exception("test");
      }
      catch (\Exception $e)
      {
       echo "Exception caught!";
      }
     }
    
    }
    
    and this catches the exception just fine? When I replace the 'throw' by a call to an application class that I know throws an exception ( for example Cache::get('doesnexist'); ) it still works fine? Are you using an old version of FuelPHP, or perhaps of the oil package?
  • Oh my god, I'm an idiot. Sorry everyone! Keith's suggestion did actually fix the issue. After reading his reply, I prepended the backslash to the throw \Exception statement in the model. And like I said that did nothing. But I needed to prepend the backslash to the catch \Exception statement in the task... Duh. Obviously... Thanks for the help guys! Gonna go wipe the egg off of my face now... :)
  • Your Exception is probably in a different scope/namespace. Make sure you prepend a \ like \Exception.
  • Thanks the reply Keith! I tried prepending the \, but no luck - same issue. In case it helps, the line that throws the exception is the following: [line number 123] if ( $response->status != 'OK' )
    [line number 124] throw new \Exception( $response->status ); And at the command line when I run the task : $ php oil refine coords neighborhood
    Error - OVER_QUERY_LIMIT in APPPATH/classes/model/maps.php on line 124 Execution stops when the error is thrown.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion