Skip to content

Commit

Permalink
Log event on failure in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Jun 17, 2023
1 parent d55d19f commit 9ae2e9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Release Notes for Campaign

## 2.8.3 - Unreleased
### Changed
- Logged messages now include the event that was passed into a webhook when a failure is encountered ([#398](https://github.com/putyourlightson/craft-campaign/issues/398)).

### Fixed
Fixed a bug that was causing errors when updating search indexes when a sendout’s subject was not set ([#397](https://github.com/putyourlightson/craft-campaign/issues/397)).

Expand Down
30 changes: 24 additions & 6 deletions src/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ public function actionAmazonSes(): ?Response
$email = $body['bounce']['bouncedRecipients'][0]['emailAddress'];
return $this->_callWebhook('bounced', $email);
}

return $this->_asRawFailure('Event `' . ($eventType ?? '') . '` not found.');
}

return $this->_asRawFailure('Event not found.');
return $this->_asRawFailure('No event provided.');
}

/**
Expand Down Expand Up @@ -173,7 +175,11 @@ public function actionMailgun(): ?Response
return $this->_callWebhook('bounced', $email);
}

return $this->_asRawFailure('Event not found.');
if ($event) {
return $this->_asRawFailure('Event `' . $event . '` not found.');
}

return $this->_asRawFailure('No event provided.');
}

/**
Expand All @@ -198,9 +204,13 @@ public function actionMandrill(): ?Response
return $this->_callWebhook('bounced', $email);
}
}

$eventTypes = array_filter(array_map(fn($event) => $event['event'] ?? null, $events));

return $this->_asRawFailure('Event `' . implode(', ', $eventTypes) . '` not found.');
}

return $this->_asRawFailure('Event not found.');
return $this->_asRawFailure('No event provided.');
}

/**
Expand Down Expand Up @@ -250,7 +260,11 @@ public function actionPostmark(): ?Response
}
}

return $this->_asRawFailure('Event not found.');
if ($eventType) {
return $this->_asRawFailure('Event `' . $eventType . '` not found.');
}

return $this->_asRawFailure('No event provided.');
}

/**
Expand Down Expand Up @@ -278,9 +292,13 @@ public function actionSendgrid(): ?Response
return $this->_callWebhook('bounced', $email);
}
}

$eventTypes = array_filter(array_map(fn($event) => $event['event'] ?? null, $events));

return $this->_asRawFailure('Event `' . implode(', ', $eventTypes) . '` not found.');
}

return $this->_asRawFailure('Event not found.');
return $this->_asRawFailure('No event provided.');
}

/**
Expand All @@ -291,7 +309,7 @@ private function _callWebhook(string $event, string $email = null): Response
Campaign::$plugin->log('Webhook request: ' . $this->request->getRawBody(), [], Logger::LEVEL_WARNING);

if ($email === null) {
return $this->_asRawFailure('Email not found.');
return $this->_asRawFailure('No email provided.');
}

$contact = Campaign::$plugin->contacts->getContactByEmail($email);
Expand Down

0 comments on commit 9ae2e9b

Please sign in to comment.