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.

Ben Hillier
  • 2,126
  • 1
  • 10
  • 15

1 Answers1

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');
}
Alex Odenthal
  • 211
  • 1
  • 11
  • 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. – Ben Hillier Feb 24 '17 at 09:42
  • So this might help? http://stackoverflow.com/questions/2661546/php-network-getaddresses-getaddrinfo-failed-name-or-service-not-known – Alex Odenthal Feb 24 '17 at 09: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! – Ben Hillier Feb 24 '17 at 10:17