I have implemented a Service which does stuff with a RESTful API:
services:
api.clientname:
class: Drupal\my_custom_module\Api\Clientname
arguments: ['@http_client', '@messenger', '@logger.factory', '@uuid']
I'm injecting dependencies for ['@http_client', '@messenger', '@logger.factory', '@uuid']
:
- Guzzle HTTP Client
- messenger service to call
$this->messenger->addMessage()
, - logger factory to call
$this->loggerFactory->get('my_custom_module')->debug($message, $variables)
, - UUID service to generate UUID v4, per API specification.
I'm also using the StringTranslationTrait
to get $this->t()
working in my class.
All this is great so far. When it works, it works, and it logs success to the dblog. But it also needs to log failures. I'm having a problem with exception handling.
I'm trying to do something like this:
while ($apiCallAttempts < API_CALL_LIMIT && $apiSuccess === FALSE) {
$apiCallAttempts++;
try {
$accessToken = $this->obtainAccessToken($url, $postFields);
$message = "Step 1: Access Token obtained from API endpoint";
$this->loggerFactory->get('my_custom_module')->debug($message, []);
$apiSuccess = TRUE;
}
catch (Exception $e) {
// TODO: "watchdog_exception('my_custom_module', $e);".
$variables = Error::decodeException($e);
$this->loggerFactory->get('my_custom_module')->error('%type: @message in %function (line %line of %file).', $variables);
if ($apiCallAttempts === API_CALL_LIMIT && $apiSuccess === FALSE) {
// Set errorMessage for messenger service.
$errorMessage = 'Error CLIENTNAME-01: Authentication API call failed';
$message = 'Failed to sync %label with API. <br> @error after @limit attempts.';
$variables = [
'%label' => $form_id,
'@error' => $errorMessage,
'@limit' => strval($apiCallAttempts),
];
$this->messenger->addMessage($errorMessage, 'error');
$this->loggerFactory->get('dex_provider_hcp_forms')->error($message, $variables);
return $accessToken;
}
}
but it doesn't work because:
Fatal error: Trait 'Drupal\my_custom_module\Api\Exception' not found in /app/docroot/modules/custom/my_custom_module/src/Api/Clientname.php on line 66
After some Googling, I am very confused about how to add the global PHP Exception
to my Service class namespace.
Removing a use
declaration inside the class removed the "Trait not found" error.
The new error message was:
Error: Class 'Drupal\my_custom_module\Api\Error' not found
EDIT:
Both of these things were happening for the same reason. I was using Exception
and Error
inside my class without prepending them with a backward slash, causing PHP to assume that Exception
was in the same namespace as the one I declared for the file. Using \Exception
and \Error
instead resolved the issue.
The new error message is:
Error: Call to undefined method Error::decodeException()
use Exception;
inside my Class. Taking that out, and adding a backslash behind\Exception
causes the error message to go away, but now when I submit an API request to a deliberately malformed API endpoint, it just hangs and does not log the exception. I must be doing something else wrong.use Drupal\Core\Utility\Error;
. But I'm afraid the edited question is even more unclear what you are actually asking.