Skip to content

Commit

Permalink
Merge pull request #7 from 1drop/task/PRS-358_improve-error-handling-…
Browse files Browse the repository at this point in the history
…on-hubspot-forms

[TASK][PRS-358] improve error-handling on hubspot forms
  • Loading branch information
Hans Höchtl authored Mar 26, 2020
2 parents 505ed05 + f67ddf1 commit ed8890a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
6 changes: 2 additions & 4 deletions Classes/Form/Finisher/HubSpotFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ protected function executeInternal()
$hubspotFormId = $formRuntime->getFormDefinition()->getIdentifier();
$formSubmitResponse = $this->hubspotFormService->submit($hubspotFormId, $hubspotFormData);

if (!empty($formSubmitResponse['inlineMessage'])) {
$formRuntime->getResponse()->setContent($formSubmitResponse['inlineMessage']);
$this->finisherContext->cancel();
}
$formRuntime->getResponse()->setContent($formSubmitResponse);
$this->finisherContext->cancel();
}

/**
Expand Down
21 changes: 12 additions & 9 deletions Classes/Service/HubspotFormService.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getFormByIdentifier(string $formIdentifier = null): array
return [];
}
if (200 !== $response->getStatusCode()) {
$this->systemLogger->log('Hubspot API returned non 200 code', LOG_ERR, ['responseCode' => $response->getStatusCode()]);
$this->logError('Hubspot API returned non 200 code', $response->getStatusCode());
return [];
}

Expand All @@ -153,23 +153,26 @@ public function getFormByIdentifier(string $formIdentifier = null): array
* @param string $formIdentifier
* @param array $formData
* @throws \Neos\Cache\Exception
* @return mixed
* @return string
*/
public function submit(string $formIdentifier, array $formData)
{
try {
$apiResponse = $this->forms->submit($this->settings['api']['portalId'], $formIdentifier, $formData);
switch ($apiResponse->getStatusCode()) {
switch ($code = $apiResponse->getStatusCode()) {
case 204:
return $this->getFormByIdentifier($formIdentifier);
case 302:
case 500:
return $this->getFormByIdentifier($formIdentifier)['inlineMessage'];
default:
$this->logError('Hubspot API returned unexpected code on form submission', $code);
return 'Something went wrong with the form submission.';
}
} catch (BadRequest $exception) {
if (400 === $exception->getCode()) {
// Validation failed.
}
$this->logError($exception->getMessage(), $exception->getCode());
return $exception->getMessage();
}
}

private function logError($message, $code) {
$this->systemLogger->log($message, LOG_ERR, ['responseCode' => $code]);
}
}

0 comments on commit ed8890a

Please sign in to comment.