You are here

protected function ClientManager::getExceptionMessage in Acquia Content Hub 8

Obtains the appropriate exception message for the selected exception.

This is the place to set up exception messages per request.

Parameters

string $request: The Request to Plexus, as defined in the content-hub-php library.

array $args: The Request arguments.

object $ex: The Exception object.

array $exception_messages: The array of messages to overwrite, keyed by Exception name.

object|void $response: The response to the request.

Return value

null|string The text to write in the messages.

1 call to ClientManager::getExceptionMessage()
ClientManager::createRequest in src/Client/ClientManager.php
Makes an API Call Request to Acquia Content Hub, with exception handling.

File

src/Client/ClientManager.php, line 403

Class

ClientManager
Provides a service for managing pending server tasks.

Namespace

Drupal\acquia_contenthub\Client

Code

protected function getExceptionMessage($request, array $args, $ex, array $exception_messages = [], $response = NULL) {

  // Obtain the class name.
  $exception = implode('', array_slice(explode('\\', get_class($ex)), -1));
  switch ($exception) {
    case 'ServerException':
      if (isset($exception_messages['ServerException'])) {
        $msg = $exception_messages['ServerException'];
      }
      else {
        $msg = new FormattableMarkup('Could not reach the Content Hub. Please verify your hostname and Credentials. [Error message: @msg]', [
          '@msg' => $ex
            ->getMessage(),
        ]);
      }
      break;
    case 'ConnectException':
      if (isset($exception_messages['ConnectException'])) {
        $msg = $exception_messages['ConnectException'];
      }
      else {
        $msg = new FormattableMarkup('Could not reach the Content Hub. Please verify your hostname URL. [Error message: @msg]', [
          '@msg' => $ex
            ->getMessage(),
        ]);
      }
      break;
    case 'ClientException':
    case 'BadResponseException':
    case 'ServerErrorResponseException':
      if (isset($exception_messages[$exception])) {
        $msg = $exception_messages[$exception];
      }
      else {
        if (isset($response) && isset($response['error'])) {

          // In the case of ClientException there are custom message that need
          // to be set depending on the request.
          $error = $response['error'];
          switch ($request) {

            // Customize the error message per request here.
            case 'register':
              $client_name = $args[0];
              $msg = new FormattableMarkup('Error registering client with name="@name" (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@name' => $client_name,
                '@error_message' => $error['message'],
              ]);
              break;
            case 'getClientByName':

              // If status code = 404, then this site name is available.
              $code = $ex
                ->getResponse()
                ->getStatusCode();
              if ($code == 404) {

                // All good! client name is available!
                return FALSE;
              }
              else {
                $msg = new FormattableMarkup('Error trying to connect to the Content Hub" (Error Code = @error_code: @error_message)', [
                  '@error_code' => $error['code'],
                  '@error_message' => $error['message'],
                ]);
              }
              break;
            case 'addWebhook':
              $webhook_url = $args[0];
              $msg = new FormattableMarkup('There was a problem trying to register Webhook URL = %URL. Please try again. (Error Code = @error_code: @error_message)', [
                '%URL' => $webhook_url,
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'deleteWebhook':

              // This function only requires one argument (webhook_uuid), but
              // we are using the second one to pass the webhook_url.
              $webhook_url = isset($args[1]) ? $args[1] : $args[0];
              $msg = new FormattableMarkup('There was a problem trying to <b>unregister</b> Webhook URL = %URL. Please try again. (Error Code = @error_code: @error_message)', [
                '%URL' => $webhook_url,
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'purge':
              $msg = new FormattableMarkup('Error purging entities from the Content Hub [Error Code = @error_code: @error_message]', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'readEntity':
              $uuid = $args[0];
              $msg = new FormattableMarkup('Entity with UUID="@uuid" was referenced by another entity, but could not be found in Content Hub. To resolve this warning, enable publishing of that entity type on the publishing site and re-export the entity. (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@uuid' => $uuid,
                '@error_message' => $error['message'],
              ]);
              break;
            case 'createEntity':
              $msg = new FormattableMarkup('Error trying to create an entity in Content Hub (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'createEntities':
              $msg = new FormattableMarkup('Error trying to create entities in Content Hub (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'updateEntity':
              $uuid = $args[1];
              $msg = new FormattableMarkup('Error trying to update an entity with UUID="@uuid" in Content Hub (Error Code = @error_code: @error_message)', [
                '@uuid' => $uuid,
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'updateEntities':
              $msg = new FormattableMarkup('Error trying to update some entities in Content Hub (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'deleteEntity':
              $uuid = $args[0];
              $msg = new FormattableMarkup('Error trying to delete entity with UUID="@uuid" in Content Hub (Error Code = @error_code: @error_message)', [
                '@uuid' => $uuid,
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            case 'searchEntity':
              $msg = new FormattableMarkup('Error trying to make a search query to Content Hub. Are your credentials inserted correctly? (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
              break;
            default:
              $msg = new FormattableMarkup('Error trying to connect to the Content Hub" (Error Code = @error_code: @error_message)', [
                '@error_code' => $error['code'],
                '@error_message' => $error['message'],
              ]);
          }
        }
        else {
          $msg = new FormattableMarkup('Error trying to connect to the Content Hub (Error Message = @error_message)', [
            '@error_message' => $ex
              ->getMessage(),
          ]);
        }
      }
      break;
    case 'RequestException':
      if (isset($exception_messages['RequestException'])) {
        $msg = $exception_messages['RequestException'];
      }
      else {
        switch ($request) {

          // Customize the error message per request here.
          case 'register':
            $client_name = $args[0];
            $msg = new FormattableMarkup('Could not get authorization from Content Hub to register client @name. Are your credentials inserted correctly? (Error message = @error_message)', [
              '@name' => $client_name,
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'createEntity':
            $msg = new FormattableMarkup('Error trying to create an entity in Content Hub (Error Message: @error_message)', [
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'createEntities':
            $msg = new FormattableMarkup('Error trying to create entities in Content Hub (Error Message = @error_message)', [
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'updateEntity':
            $uuid = $args[1];
            $msg = new FormattableMarkup('Error trying to update entity with UUID="@uuid" in Content Hub (Error Message = @error_message)', [
              '@uuid' => $uuid,
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'updateEntities':
            $msg = new FormattableMarkup('Error trying to update some entities in Content Hub (Error Message = @error_message)', [
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'deleteEntity':
            $uuid = $args[0];
            $msg = new FormattableMarkup('Error trying to delete entity with UUID="@uuid" in Content Hub (Error Message = @error_message)', [
              '@uuid' => $uuid,
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          case 'searchEntity':
            $msg = new FormattableMarkup('Error trying to make a search query to Content Hub. Are your credentials inserted correctly? (Error Message = @error_message)', [
              '@error_message' => $ex
                ->getMessage(),
            ]);
            break;
          default:
            $msg = new FormattableMarkup('Error trying to connect to the Content Hub. Are your credentials inserted correctly? (Error Message = @error_message)', [
              '@error_message' => $ex
                ->getMessage(),
            ]);
        }
      }
      break;
    case 'Exception':
    default:
      if (isset($exception_messages['Exception'])) {
        $msg = $exception_messages['Exception'];
      }
      else {
        $msg = new FormattableMarkup('Error trying to connect to the Content Hub (Error Message = @error_message)', [
          '@error_message' => $ex
            ->getMessage(),
        ]);
      }
      break;
  }
  return $msg;
}