msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Make exception handling experience intuitive

Open EmilySamantha80 opened this issue 1 year ago • 1 comments

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
  }
}

EmilySamantha80 avatar Oct 17 '22 16:10 EmilySamantha80