microsoftgraph / msgraph-sdk-php

Microsoft Graph Library for PHP.
Other
575 stars 144 forks source link

Make exception handling experience intuitive #1034

Open EmilySamantha80 opened 2 years ago

EmilySamantha80 commented 2 years ago

I'm encoutering an issue with using ApiException to get exceptions in v2.0.0 RC9. The message variable is always blank with exceptions for most commands regarding working with sites, drives, and files. The exception is in there, just buried inside of a private 'error' variable.

Example of getting a site that doesn't exist:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once __DIR__ . '/vendor/autoload.php';

use Microsoft\Graph\Generated\Models;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Kiota\Authentication\PhpLeagueAuthenticationProvider;

try {
    $tokenRequestContext = new ClientCredentialContext(
        $tenantId,
        $clientId,
        $clientSecret
    );
    $scopes = ['https://graph.microsoft.com/.default'];
    $authProvider = new PhpLeagueAuthenticationProvider($tokenRequestContext, $scopes);
    $requestAdapter = new GraphRequestAdapter($authProvider);
    $graphServiceClient = new GraphServiceClient($requestAdapter);

    /** @var Models\Site $site  */
    $site = $graphServiceClient->sitesById($siteId)->get()->wait();
} catch (ApiException $ex) {
    echo $ex->getMessage();
}

?>

This returns the a blank $ex->message, and the following var_dump($ex):

object(Microsoft\Graph\Generated\Models\ODataErrors\ODataError)#59 (9) {
  ["message":protected]=>
  string(0) ""
  ["string":"Exception":private]=>
  string(0) ""
  ["code":protected]=>
  int(0)
  ["file":protected]=>
  string(131) "/home/username/test/vendor/microsoft/microsoft-graph/src/Generated/Models/ODataErrors/ODataError.php"
  ["line":protected]=>
  int(37)
  ["trace":"Exception":private]=>
  array(7) {
    [0]=>
    array(5) {
      ["file"]=>
      string(114) "/home/username/test/vendor/microsoft/kiota-serialization-json/src/JsonParseNode.php"
      ["line"]=>
      int(107)
      ["function"]=>
      string(28) "createFromDiscriminatorValue"
      ["class"]=>
      string(55) "Microsoft\Graph\Generated\Models\ODataErrors\ODataError"
      ["type"]=>
      string(2) "::"
    }
    [1]=>
    array(5) {
      ["file"]=>
      string(114) "/home/username/test/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php"
      ["line"]=>
      int(308)
      ["function"]=>
      string(14) "getObjectValue"
      ["class"]=>
      string(48) "Microsoft\Kiota\Serialization\Json\JsonParseNode"
      ["type"]=>
      string(2) "->"
    }
    [2]=>
    array(5) {
      ["file"]=>
      string(114) "/home/username/test/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php"
      ["line"]=>
      int(88)
      ["function"]=>
      string(19) "throwFailedResponse"
      ["class"]=>
      string(41) "Microsoft\Kiota\Http\GuzzleRequestAdapter"
      ["type"]=>
      string(2) "->"
    }
    [3]=>
    array(5) {
      ["file"]=>
      string(99) "/home/username/test/vendor/php-http/promise/src/FulfilledPromise.php"
      ["line"]=>
      int(35)
      ["function"]=>
      string(30) "Microsoft\Kiota\Http\{closure}"
      ["class"]=>
      string(41) "Microsoft\Kiota\Http\GuzzleRequestAdapter"
      ["type"]=>
      string(2) "->"
    }
    [4]=>
    array(5) {
      ["file"]=>
      string(114) "/home/username/test/vendor/microsoft/kiota-http-guzzle/src/GuzzleRequestAdapter.php"
      ["line"]=>
      int(98)
      ["function"]=>
      string(4) "then"
      ["class"]=>
      string(29) "Http\Promise\FulfilledPromise"
      ["type"]=>
      string(2) "->"
    }
    [5]=>
    array(5) {
      ["file"]=>
      string(135) "/home/username/test/vendor/microsoft/microsoft-graph/src/Generated/Sites/Item/SiteItemRequestBuilder.php"
      ["line"]=>
      int(277)
      ["function"]=>
      string(9) "sendAsync"
      ["class"]=>
      string(41) "Microsoft\Kiota\Http\GuzzleRequestAdapter"
      ["type"]=>
      string(2) "->"
    }
    [6]=>
    array(5) {
      ["file"]=>
      string(64) "/home/username/test/graph-bug.php"
      ["line"]=>
      int(32)
      ["function"]=>
      string(3) "get"
      ["class"]=>
      string(59) "Microsoft\Graph\Generated\Sites\Item\SiteItemRequestBuilder"
      ["type"]=>
      string(2) "->"
    }
  }
  ["previous":"Exception":private]=>
  NULL
  ["additionalData":"Microsoft\Graph\Generated\Models\ODataErrors\ODataError":private]=>
  array(0) {
  }
  ["error":"Microsoft\Graph\Generated\Models\ODataErrors\ODataError":private]=>
  object(Microsoft\Graph\Generated\Models\ODataErrors\MainError)#80 (6) {
    ["additionalData":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    array(1) {
      ["innerError"]=>
      array(3) {
        ["date"]=>
        string(19) "2022-10-17T16:23:06"
        ["request-id"]=>
        string(36) "80ddeda1-e38d-41fc-a42d-6e943d3b16cd"
        ["client-request-id"]=>
        string(36) "7ee8f8ec-2bd6-4609-aa90-3a2356efa61f"
      }
    }
    ["code":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    string(12) "itemNotFound"
    ["details":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    NULL
    ["innererror":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    NULL
    ["message":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    string(33) "Requested site could not be found"
    ["target":"Microsoft\Graph\Generated\Models\ODataErrors\MainError":private]=>
    NULL
  }
}
Ndiritu commented 2 years ago

@EmilySamantha80 thanks for your feedback on our Release Candidate. Please keep it coming.

For now, you can get the error returned by the API using $ex->getError() which will expose the message and further details.

The exception handling experience can definitely be improved so I'll keep this open to track that