docusign / docusign-esign-php-client

The Official Docusign PHP Client Library used to interact with the eSign REST API. Send, sign, and approve documents using this client.
https://www.docusign.com/devcenter
MIT License
198 stars 122 forks source link

Get the error message when a 401 occurs #127

Closed khalyomede closed 3 years ago

khalyomede commented 3 years ago

I am trying to send an envelope, and the code throws a 401 error:

DocuSign\eSign\ApiException: [401] Error connecting to the API (https://eu.docusign.net/restapi/v2/accounts/500369231/envelopes) in /path/to/my/app/vendor/docusign/esign-client/src/ApiClient.php:286

The issue is that I don't know why I am getting this error, since in the code, on line 2872 of file vendor/docusign/esign-client/src/Api/EnvelopesApi.php, throws an ApiException...

<?php

class CreateEnvelopeOptions
{
    public function createEnvelopeWithHttpInfo($account_id, $envelope_definition = null, EnvelopesApi\CreateEnvelopeOptions $options = null)
    {
        // [...]
        // make the API Call
        try {
            list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
                $resourcePath,
                'POST',
                $queryParams,
                $httpBody,
                $headerParams,
                '\DocuSign\eSign\Model\EnvelopeSummary',
                '/v2/accounts/{accountId}/envelopes'
            );

            return [$this->apiClient->getSerializer()->deserialize($response, '\DocuSign\eSign\Model\EnvelopeSummary', $httpHeader), $statusCode, $httpHeader];
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 201:
                    $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\DocuSign\eSign\Model\EnvelopeSummary', $e->getResponseHeaders());
                    $e->setResponseObject($data);
                    break;
                case 400:
                    $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '\DocuSign\eSign\Model\ErrorDetails', $e->getResponseHeaders());
                    $e->setResponseObject($data);
                    break;
            }

===========>throw $e;
        }
    }
}

...and in line 287 of file vendor/docusign/esign-client/src/ApiClient.php, it shadows the cURL error message (only setting the erorr status code in the exception message):

<?php

class ApiClient
{
    public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
    {
        // [...]
        // Handle the response
        if ($response_info['http_code'] === 0) {
            $curl_error_message = curl_error($curl);

            // curl_exec can sometimes fail but still return a blank message from curl_error().
            if (!empty($curl_error_message)) {
                $error_message = "API call to $url failed: $curl_error_message";
            } else {
                $error_message = "API call to $url failed, but for an unknown reason. " .
                        "This could happen if you are disconnected from the network.";
            }

            $exception = new ApiException($error_message, 0, null, null);
            $exception->setResponseObject($response_info);
            throw $exception;
        } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) {
            // return raw body if response is a file
            if ($responseType === '\SplFileObject' || $responseType === 'string') {
                return [$http_body, $response_info['http_code'], $http_header];
            }

            $data = json_decode($http_body);
            if (json_last_error() > 0) { // if response is a string
                $data = $http_body;
            }
        } else {
            $data = json_decode($http_body);
            if (json_last_error() > 0) { // if response is a string
                $data = $http_body;
            }

            throw new ApiException(
===============>"[".$response_info['http_code']."] Error connecting to the API ($url)",
                $response_info['http_code'],
                $http_header,
                $data
            );
        }
        return [$data, $response_info['http_code'], $http_header];
    }
}

How can I get the error message to know what is wrong with my setup?

ivdinkovds commented 3 years ago

@khalyomede, you can try reading what is returned to $data = json_decode($http_body). In your case I can see only one error popping up all the time "IN_PERSON_SIGNING_HOST_MUST_BE_VALID_USER". I hope this helps

khalyomede commented 3 years ago

Thanks @ivdinkovds, we figured out this was an issue with our job queues maintaining an old user that have been changed. We managed to fix our already queued up jobs by changing the uuid of the user and it worked well since then.