jsoumelidis / php_sap

PHP (5.6, 7.x) extension for SAP Remote Function Modules invocation
GNU General Public License v2.0
5 stars 1 forks source link

Error handlling if connection couldn't be made #5

Closed durlaniulian closed 6 years ago

durlaniulian commented 6 years ago

Hello @jsoumelidis ,

Thanx a lot for all your help since now. Now I have a problem when i can't connect to sap via rfc using "sap_connect()". If connection is done i get a resource number, but i don't have the error control when i can't connect: "Fatal error: Uncaught SapConnectionException: LOCATION CPIC (TCP/IP) on local host with Unicode ERROR" When i get this error i want to pop-up an alert with another text.

Can you help me please?

Gratefully, Iulian.

jsoumelidis commented 6 years ago

Hi @durlaniulian , You should be able to catch the exception, try something like this:

<?php
try {
    $connection = sap_connect(...);
} catch (SapException $e) {
    //popup alert logic here...
    //or just echo the exception message:
    echo $e->getMessage();
}
durlaniulian commented 6 years ago

Thx a lot. I tried this once but certainly i omitted something.

jsoumelidis commented 6 years ago

Do you still have problem? If so, could you paste your code?

durlaniulian commented 6 years ago

No no. It's working. Now I'm trying to figure how can i get the message number to do a case statement.

durlaniulian commented 6 years ago

object(SapConnectionException)#1 (16) { ["message":protected]=> string(405) " LOCATION CPIC (TCP/IP) on local host with Unicode ERROR partner 'XXXXXXXXXXXX' not reached TIME Fri Jun 22 11:56:41 2018 RELEASE 720 COMPONENT NI (network interface) VERSION 40 RC -10 MODULE nixxi.cpp LINE 3283 DETAIL NiPConnect2: XXXXXXXXXXXXX SYSTEM CALL connect ERRNO 10060 ERRNO TEXT WSAETIMEDOUT: Connection timed out COUNTER 21 " ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(1) ["file":protected]=> string(59) "C:\xampp\htdocs\fingerprint2\SAP_CONNECT\test conexiune.php" ["line":protected]=> int(6) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(59) "C:\xampp\htdocs\fingerprint2\SAP_CONNECT\test conexiune.php" ["line"]=> int(6) ["function"]=> string(11) "sap_connect" ["args"]=> array(1) { [0]=> array(7) { ["ashost"]=> string(29) "XXXXXXXXXXX" ["client"]=> string(3) "XXXXXX" ["sysnr"]=> string(2) "XXXXX" ["user"]=> string(3) "XXXXX" ["passwd"]=> string(9) "XXXXXXXXXX" ["lang"]=> string(2) "XXXXX" ["trace"]=> int(0) } } } } ["previous":"Exception":private]=> NULL ["MSGTY":protected]=> string(0) "" ["MSGID":protected]=> string(0) "" ["MSGNO":protected]=> string(0) "" ["MSGV1":protected]=> string(0) "" ["MSGV2":protected]=> string(0) "" ["MSGV3":protected]=> string(0) "" ["MSGV4":protected]=> string(0) "" ["KEY":protected]=> string(25) "RFC_COMMUNICATION_FAILURE" ["nwsdkfunction":protected]=> string(17) "RfcOpenConnection" }

durlaniulian commented 6 years ago

Sorry for putting all error message but can you help me to extract de message error? (10060 is what i need)

Here is my code: $sapConfig = include DIR . DIRECTORY_SEPARATOR . 'sap_config.php';

try { $connection = sap_connect($sapConfig); } catch (SapException $e) { //popup alert logic here... //or just echo the exception message: $e->getMessage(); var_dump($e); }

jsoumelidis commented 6 years ago

You can use regular expression to extract the info you want from the message, but before doing that, maybe the exception key is enough?

<?php
try {
    $connection = sap_connect(...);
} catch (SapException $e) {
    //Check the exception key
    if ($e->getMessageKey() === 'RFC_COMMUNICATION_FAILURE' ) {
        //popup alert logic here...
    }
    //or just echo the exception message:
    echo $e->getMessage();
}
durlaniulian commented 6 years ago

SapConnectionException: LOCATION CPIC (TCP/IP) on local host with Unicode ERROR partner 'XXXXXXX' not reached TIME Fri Jun 22 12:19:49 2018 RELEASE 720 COMPONENT NI (network interface) VERSION 40 RC -10 MODULE nixxi.cpp LINE 3283 DETAIL NiPConnect2: XXXXXX SYSTEM CALL connect ERRNO 10060 ERRNO TEXT WSAETIMEDOUT: Connection timed out COUNTER 29 in C:\xampp\htdocs\fingerprint2\SAP_CONNECT\test conexiune.php:6 Stack trace: #0 C:\xampp\htdocs\fingerprint2\SAP_CONNECT\test conexiune.php(6): sap_connect(Array) #1 {main}

From this message i want to get only "10060" to be easier for me to show different messages, because this error number can change and i want to show another message.

jsoumelidis commented 6 years ago

You can try regular expressions.

durlaniulian commented 6 years ago

I solved it by other method. I converted the message in string and if his length is bigger than 0 i show another message . Thanks a lot.