Welcome Guest, Not a member yet? Register   Sign In
Library fatal error handling
#1

[eluser]billmce[/eluser]
I have a curl routine in my model.
If the destination site isn't available I get the following error returned and the application dies.

Quote:Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\cif\application\libraries\Curl.php on line 255

I don't seem to be able to catch this error at the codeigniter level.
I'm looking for a more elegant solution.

Because the model isn't returning valid data I know 'something is wrong' and tried to handle it with a view indicating a problem .... but the codeigniter commands don't work after this error ... so I resorted to the echo's shown.

Code:
if($ticket == "<" or $ticket == '')
     {
        //the echo statements show ... but the codeigniter stuff does not
         echo "Unable to reach the Document Management System.<br />";
         echo "Please alert the System Administrator.<br />";
         echo "To continue ... without access to documents ... hit the 'BACK' button of your browser";
         //we have to do something here ... send to a odd page,
         // or don't bring up the upload section show "not available" ... yeah
         // do the same for the search parts.
         // redirect to error page.
         $data['error_msg'] = 'Unable to contact Document Management System.<br />';
         $data['error_msg'] .= 'Please contact System Administrator.<br />';
        // 'return to' link on error view and as redirect
        $data['return_page']=current_url();
        $data['redirect_to'] = current_url();
        
        $data['robots'] = '&lt;meta name="robots" content="noindex,nofollow"&gt;';
        $data['title']="System Error";  //shows up on tab
        $data['pagetitle']="Network Issue -- Document Management System";  //top of page body
        $this->load->view('header_view',$data);
        $this->load->view('menu_view');
        $this->load->view('form_error_view', $data);  
        $this->load->view('footer_view',$data);  
        
     }

Is there a better way of handling errors from libraries?
TIA
#2

[eluser]mddd[/eluser]
If I understand correctly, your Curl request is taking so long that your php script times out.
You should be able to use CURLOPT_TIMEOUT to tell Curl to take a maximum amount of time (say, 45 seconds).
That way Curl will time out, but your script will continue. So you can take appropriate measures.
#3

[eluser]billmce[/eluser]
I tried CURL_OPT timeout (see below)

The problem, is that the views on the controller (shown above in the original post) aren't executed ... only the echo's immediately above them.

For the sake of completeness ... here's the model where the curl is initiated.

Code:
function get_ticket()
    {
        $ticket = ''; //init  
        $credentials = json_encode($this->_credentials());
        
        $ch = curl_init( $this->config->item('alf_server') .  'alfresco/service/api/login');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $credentials);
        curl_setopt($ch, CURLOPT_TIMEOUT,25);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/html'));
        
        $postResult = curl_exec($ch);
        
        // Check if any error occured
        if(curl_errno($ch))
        {
            $ticket = ''; //no ticket.
        }
        else
        {        
            $ticket = json_decode($postResult)->data->ticket;  //set ticket to return
        }
        curl_close($ch);
        return $ticket;
    }
#4

[eluser]mddd[/eluser]
I can't believe that the Curl call timing out is somehow causing the Codeigniter calls not to happen.
Are you using caching or some other mechanism that prevents the Codeigniter views from being displayed?
If you are not seeing your views, but also not getting errors, than it is not a question of 'codeigniter commands not working'.
If they didn't work at all you would get errors.

Try setting one of the 'load->view' commands to a view that doesn't exist. If you get an error there, you know that the
views ARE being loaded. Only something else (like caching) is preventing them from being displayed?
#5

[eluser]billmce[/eluser]
I'm not using caching.
To the best of my knowledge I'm not doing anything to prevent the views displaying.

I did create a view 'does_not_exist.php' and inserting it into the controller yielded interesting results:
Quote:Unable to reach the Document Management System.
Please alert the System Administrator.
To continue ... without access to documents ... hit the 'BACK' button of your browser
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\cif\system\libraries\Exceptions.php:166)

Filename: codeigniter/Common.php

Line Number: 360
An Error Was Encountered

Unable to load the requested file: does_not_exist_view.php

The 'A PHP Error was encountered' and 'An Error Was Encountered' messages are both 'boxed' messages as we're accustomed to seeing in code igniter.

Thanks for the help
#6

[eluser]mddd[/eluser]
Okay, so it looks like you are not seeing any views because there is some kind of redirection going on.
Something is trying to send http headers and that is usually a 'redirect'.
Probably, if you take out the 'missing view', the page will redirect and that is why you don't see your view information.
That is my guess. So: check where you might be doing a redirect (or other type of header).
#7

[eluser]billmce[/eluser]
Removing the non-existent view didn't show the results of a redirect. It just showed the echo statements.

Check my understanding.
You're saying that something has already written the headers for the next page ... and as a result the views that I want to display are being ignored?

I've looked at the redirects in the controller and none of the conditions for a redirect are being met. Isn't a redirect immediate? If I had a redirect in play I'd never get to my echo's, right?

Confused.

Interesting side note, if I add a redirect after the echo statements then I do get redirected as expected.




Theme © iAndrew 2016 - Forum software by © MyBB