omniphx / forrest

A Laravel library for Salesforce
https://omniphx.github.io/forrest/
MIT License
257 stars 120 forks source link

failed exception handling with try/catch for SF error "invalid field" #277

Open Shaws-WD opened 3 years ago

Shaws-WD commented 3 years ago

Short version I am encountering a problem with Forrest v2.11 under Laravel 6.20.8 The problem occurs when the Forrest call $response = Forrest::query($SOQL) is embedded within a try/catch. An error results in execution halting without catching an exception

More details You can reproduce the error with the following code

    private function testForrestBug ()   //CustomField
    {

        $arrData = ['Companyx' => 'TestWDASCompany',
            'FirstName' => 'TestWDASFirst',
            'LastName' => 'TestWDASFirstLast'
            ];
        $sObjectName = 'Lead';

        log::debug('before Forrest::sobjects');
        try {
            $response = Forrest::sobjects($sObjectName,[
                'method' => 'post',
                'body'   => $arrData
            ]);
            log::debug('after Forrest::sobjects');
            log::debug($response);
        } catch (SalesforceException $exception) {
            $msg = 'SalesforceException exception:' & $exception->getMessage(); log::debug($msg); return;
        } catch (Exception $exception) {
            $msg = 'non SalesforceException exception:' & $exception->getMessage(); log::debug($msg);return;
        }
        $Forrest_success = $response['success'] ?? null;
        if (!$Forrest_success) {
            $msg = $sObjectName.' addition failed without exception - when does this occur? '; log::debug($msg);    
            return;         
        } else {
            $msg = $sObjectName.' addition suceeded '; log::debug($msg);    return;         
        }
        return;
    }

Without the try/catch the call to Salesforce will fail with a logged error of

6:19:33] local.ERROR: [
    {
        "message": "No such column 'Companyx' on sobject of type Lead",
        "errorCode": "INVALID_FIELD"
    }
] {"userId":231,"exception":"[object] (Omniphx\\Forrest\\Exceptions\\SalesforceException(code: 400): [
    {
        \"message\": \"No such column 'Companyx' on sobject of type Lead\",
        \"errorCode\": \"INVALID_FIELD\"
    }
] at /var/www/wd-dev/vendor/omniphx/forrest/src/Omniphx/Forrest/Client.php:835)
[stacktrace]

But with the try/catch the execution HALTS without catching an exception. Laravel logs a bizarre pair of lines which I have represented below (substituting hex character representation)

J
  <0x00> h<0x00>   <0x00> "e`paaf  8  @d cd"  

This resembles the error which I reported previously, but it is now after upgrade to latest version of Forrest and with better documentation of the error. The previous error was
failed exception handling when "The users password has expired" #261

mbroadhead commented 2 years ago

You are passing $exception->getMessage() by reference to another string and then logging the resulting string. I think you meant to concatenate $exception->getMessage().

Patch your code with this:

--- /tmp/a      2022-04-07 09:27:07.643822898 -0600
+++ /tmp/b      2022-04-07 09:27:29.883798919 -0600
@@ -16,9 +16,9 @@
                        log::debug('after Forrest::sobjects');
                        log::debug($response);
                } catch (SalesforceException $exception) {
-                       $msg = 'SalesforceException exception:' & $exception->getMessage(); log::debug($msg); return;
+                       $msg = 'SalesforceException exception:' . $exception->getMessage(); log::debug($msg); return;
                } catch (Exception $exception) {
-                       $msg = 'non SalesforceException exception:' & $exception->getMessage(); log::debug($msg);return;
+                       $msg = 'non SalesforceException exception:' . $exception->getMessage(); log::debug($msg);return;
                }
                $Forrest_success = $response['success'] ?? null;
                if (!$Forrest_success) {

The one character bugs are the hardest to find!