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

Error handler returns nothing for 400 errors #521

Open
rgasch opened this issue Sep 18, 2024 · 4 comments
Open

Error handler returns nothing for 400 errors #521

rgasch opened this issue Sep 18, 2024 · 4 comments

Comments

@rgasch
Copy link

rgasch commented Sep 18, 2024

We have fundamentally working integration. However, sometimes we get a 400 Error and in this case, the following code

$errorHandler = $this->_connector->getLastError();
CliLog::error("API: Http Status Code = " . $errorHandler->getHttpStatusCode());
CliLog::error("API: Error Message = " . $errorHandler->getIntuitErrorMessage());
CliLog::error("API: Error Element = " . $errorHandler->getIntuitErrorElement());
CliLog::error("API: Error Detail = " . $errorHandler->getIntuitErrorDetail());
CliLog::error("API: Error Code = " . $errorHandler->getIntuitErrorCode());
CliLog::error("API: Error Type = " . $errorHandler->getIntuitErrorType());

All these calls other than the getHttpStatusCode() call return nothing.

Is this a bug or are we using an incorrect method to get the cause of a 400 error?

@kodnificent
Copy link

Experiencing this same issue

@sashidharg
Copy link

sashidharg commented Sep 20, 2024

Have the same problem.

This might be because the code expects the quickbooks response header as "application/xml" while the actual response has "application/xml;charset=UTF-8".

The code at IntuitResponse::setFaultHandler() expects "application/xml".

if($this->getResponseContentType() != null && (strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_APPLICATIONXML) == 0 || strcasecmp($this->getResponseContentType(), CoreConstants::CONTENTTYPE_TEXTXML) == 0)){ $this->faultHandler->parseResponse($body); }

@rgasch
Copy link
Author

rgasch commented Sep 23, 2024

OK, I tried to figure out how to fix this, but didn' t get all that far, since for some reason the parsing fails, there's nothing more to
work with using the available SDK code. However, I have a workaround:

When you call your API Client function - in this example it's Add() - do this:

return $this->_getConnector()->throwExceptionOnError(true)->Add($entity);

Then in the code that calls this function, switch to try/catch:

try {
	$response = $this->add($object);
} catch (\Exception $e) {
	$this->_printApiError($e);
	return null;
}

Finally, here's the method that parses the exception text using the Laravel Str helper class in a very quick and dirty manner, I spent like 5 minutes writing this:

	private function _printApiError(\Exception $e): int
	{
		$xmlString = Str::between($e->getMessage(), '">', '</IntuitResponse>');
		$xml       = new \SimpleXMLElement($xmlString);

		// Extract error details
		$errorCode    = (int) $xml->Error['code'];
		$errorMessage = (string) $xml->Error->Message;
		$errorDetail  = (string) $xml->Error->Detail;

		// Log the extracted error details
		CliLog::error("API: Http Status Code = " . $errorCode);
		CliLog::error("API: Error Code = " . $errorCode);
		CliLog::error("API: Error Message = " . $errorMessage);
		CliLog::error("API: Error Detail = " . $errorDetail);

		return $errorCode;
	}

Like I said, this is quick and dirty and could certainly be improved, but at least now you're able to see what error you got and work with this info.

@kodnificent
Copy link

This worked for me.

$error = $this->data_service->getLastError();

if ($error) {
   $err = $error->getResponseBody();
}

getIntuitErrorMessage() was returning an empty string. With the solution above, I was able to get the error from the response body

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

3 participants