diff -u b/src/Entity/PardotSubmission.php b/src/Entity/PardotSubmission.php --- b/src/Entity/PardotSubmission.php +++ b/src/Entity/PardotSubmission.php @@ -57,8 +57,8 @@ /** * {@inheritdoc} */ - public function getStatus($status) { - return $entity->status->value; + public function getStatus() { + return $this->status->value; } /** diff -u b/src/Entity/PardotSubmissionInterface.php b/src/Entity/PardotSubmissionInterface.php --- b/src/Entity/PardotSubmissionInterface.php +++ b/src/Entity/PardotSubmissionInterface.php @@ -35,7 +35,7 @@ * Provides the status of a pardot submission entity. * * @return string - * The status string. + * The pardot submission status. */ public function getStatus(); diff -u b/src/PardotHandler.php b/src/PardotHandler.php --- b/src/PardotHandler.php +++ b/src/PardotHandler.php @@ -127,7 +127,7 @@ } /** - * Get response data. + * Process the Pardot response data and updates the pardot submission entity. * * @param \Psr\Http\Message\ResponseInterface $response * The response returned by the remote server. @@ -142,19 +142,43 @@ PardotSubmissionInterface $pardot_submission ) { $status_code = $response->getStatusCode(); + $log = $response->getBody()->getContents(); + $status = self::PARDOT_PROCESSED; - // Log error message if status code is not 2xx. - if ($status_code < 200 || $status_code >= 300) { - $pardot_submission->addLog('An unexpected error occured while submitting data to Pardot'); - $pardot_submission->setStatus(self::PARDOT_ERROR); - $pardot_submission->save(); + // The pardot response would include text `field is required` if a form + // validation error happens at Pardot. + if (strpos($log, 'field is required') !== FALSE) { + $log = 'Could not post data to pardot, If a field is required on your + pardot form handler, make the field required on the webform'; + $status = self::PARDOT_ERROR; + } + + // Log error message depending on the status code. + switch ($status_code) { + // 5xx status code would mean an error at Pardot end. + case $status_code < 510 && $status_code >= 500: + $log = 'Internal Server Error at Pardot'; + $status = self::PARDOT_ERROR; + break; - return $pardot_submission; + // If status code is other than 500 or 200 log a generic error message + // with the status code. + case $status_code < 200 || $status_code >= 300: + $log = 'An unexpected error occured while submitting data to Pardot'; + $status = self::PARDOT_ERROR; + break; } - $pardot_submission->addLog($response->getBody()->getContents()); - $pardot_submission->setStatus(self::PARDOT_PROCESSED); + // Append status code along with the log message. + $log = $status_code . ': ' . $log; + + // Make sure that the log only contains 500 characters. + $log = substr($log, 0, 500); + + $pardot_submission->addLog($log); + $pardot_submission->setStatus($status); $pardot_submission->save(); + return $pardot_submission; } diff -u b/tests/src/Unit/PardotHandlerTest.php b/tests/src/Unit/PardotHandlerTest.php --- b/tests/src/Unit/PardotHandlerTest.php +++ b/tests/src/Unit/PardotHandlerTest.php @@ -118,9 +118,9 @@ $pardot_submission->getWebformSubmission() ->willReturn($this->mockWebformSubmission()); - $pardot_submission->addLog("Succesfull") + $pardot_submission->addLog("200: Succesfull") ->willReturn($this->mockPardotSubmissionEntity()); - $pardot_submission->addLog("An unexpected error occured while submitting data to Pardot") + $pardot_submission->addLog("503: Internal Server Error at Pardot") ->willReturn($this->mockPardotSubmissionEntity()); $pardot_submission->setStatus("processed")