Skip to content

Commit

Permalink
Fix responding to event webhooks and improve error reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmollenhour committed Jun 13, 2024
1 parent 467e1b1 commit 6f2e5cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
9 changes: 3 additions & 6 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,9 @@ public function subscribe()
/**
* Have the plugin respond to an event
*
* @param string $topic
* @param array $message
* @return void
* @throws Exception
*/
public function respond($topic, $message)
public function respond(string $topic, Varien_Object $message): void
{
list($resource, $event) = explode(':', $topic, 2);
if ( ! $this->getConfig("plugin/{$this->_plugin}/events/{$resource}/{$event}") && $topic != 'test:ping') {
Expand Down Expand Up @@ -364,10 +361,10 @@ public function log($message, $destination = NULL)
/**
* Write exception to log
*
* @param Exception $e
* @param Throwable $e
* @return void
*/
public function logException(Exception $e)
public function logException(Throwable $e)
{
$this->log("\n" . $e->__toString());
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Varien/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ public function debug($data=NULL, &$objects=array())
* @param string $offset
* @param mixed $value
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->_data[$offset] = $value;
Expand All @@ -765,6 +766,7 @@ public function offsetSet($offset, $value)
* @param string $offset
* @return boolean
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->_data[$offset]);
Expand All @@ -776,6 +778,7 @@ public function offsetExists($offset)
* @link http://www.php.net/manual/en/arrayaccess.offsetunset.php
* @param string $offset
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->_data[$offset]);
Expand All @@ -788,6 +791,7 @@ public function offsetUnset($offset)
* @param string $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->_data[$offset]) ? $this->_data[$offset] : NULL;
Expand Down
4 changes: 3 additions & 1 deletion pub/respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
if ( ! $message) {
throw new Exception('Message not specified.', 400);
}
$message = new Varien_Object($message);

$middleware->log(sprintf('Received webhook for %s topic with message: %s', $topic, $json), 'webhooks.log');
$middleware->respond($topic, $message);
} catch (Exception $e) {
} catch (Throwable $e) {
if (empty($middleware)) {
error_log($e->getMessage());
} else {
$middleware->logException($e);
}
http_response_code($e->getCode() ?: 500);
echo "ERROR: {$e->getMessage()}";
}

0 comments on commit 6f2e5cc

Please sign in to comment.