Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Shaws-WD opened this issue Jan 9, 2021 · 1 comment
Open

Comments

@Shaws-WD
Copy link

Shaws-WD commented Jan 9, 2021

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
Copy link

mbroadhead commented Apr 7, 2022

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants