1

I was trying to send a document to a printer. This is the code I used:

try
        {

            $fp  = pfsockopen($ip, $port,$errno,$errstr,30);
            fputs($fp, $sContent);
            fclose($fp);

            $this->session->set_flashdata('message_header','Document verstuurd');
            $this->session->set_flashdata('message_status','info');
            $this->session->set_flashdata('message', 'Uw document is verstuurd naar de printer');
            $this->redirect('/goods_receipt/history');


        }
        catch (Exception $e) 
        {
            $this->session->set_flashdata('message',$e->getMessage());
            $this->session->set_flashdata('message_status','danger');
            $this->session->set_flashdata('message_header', 'Er is een fout opgetreden');
            $this->redirect('/goods_receipt/history');
        }

Silly old me forgot to set the IP address. I would have expected an exception and then the flash message to be shown, but instead my customer saw this:

enter image description here

What am I doing wrong?

p.s.: Please note I don't need any help finding why the error was thrown. I've worked that out. I just need to understand why the exception handler got ignored so that next time the socket can't be opened, a rather less scary screen gets shown.

1 Answer 1

1

pfsockopen simply returns false in case of an error and is not throwing an error. http://php.net/manual/de/function.fsockopen.php

do something like

if($fp===false){
 throw new Exception('Less Scary Message');
}
3
  • Thanks for the response! My German is pretty flaky, but the English equivalent is easy to find. It would be workable if it did what is said on that page, and return an error, but it doesn't. It just drops out with the message. Commented Feb 24, 2017 at 9:42
  • So this might help? stackoverflow.com/questions/2661546/… Commented Feb 24, 2017 at 9:48
  • I'll accept this answer because it's the closest I'm going to get :-) . I guess the real conclusion is that pfsockopen doesn't throw exceptions. Thanks for your time! Commented Feb 24, 2017 at 10:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.