Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/Core/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
namespace Microsoft\Graph\Core;

use Microsoft\Graph\Exception\GraphException;

/**
* Class Enum
*
Expand All @@ -41,12 +39,12 @@ abstract class Enum
*
* @param string $value The value of the enum
*
* @throws GraphException if enum value is invalid
* @throws \InvalidArgumentException if enum value is invalid
*/
public function __construct($value)
{
if (!self::has($value)) {
throw new GraphException("Invalid enum value $value");
throw new \InvalidArgumentException("Invalid enum value $value");
}
$this->_value = $value;
}
Expand Down
55 changes: 55 additions & 0 deletions src/Exception/BaseErrorContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/


namespace Microsoft\Graph\Exception;

/**
* Class Error
*
* Defines common Error class logic
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class BaseErrorContent
{
/**
* Contains all properties of the error response
*
* @var array
*/
private $propDict;

public function __construct(array $propDict) {
$this->propDict = $propDict;
}

/**
* Returns all properties passed to the constructor
*
* @return array
*/
public function getProperties(): array {
return $this->propDict;
}

/**
* Returns value of $property in $propDict
*
* @param $property
* @return mixed|null
*/
protected function getProperty($property) {
if (array_key_exists($property, $this->propDict)) {
return $this->propDict[$property];
}
return null;
}
}
5 changes: 4 additions & 1 deletion src/Exception/GraphClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@

/**
* Class GraphClientException
*
* Thrown when the Graph API returns 4xx responses
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class GraphClientException extends GraphException
class GraphClientException extends GraphResponseException
{

}
66 changes: 66 additions & 0 deletions src/Exception/GraphErrorContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/


namespace Microsoft\Graph\Exception;

/**
* Class GraphError
*
* Contains Graph service specific error content defined in the "innerError" of an ODataError
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class GraphErrorContent extends ODataErrorContent
{
/**
* Returns the date of the request
*
* @return string|null
*/
public function getDate(): ?string {
return $this->getProperty("date");
}

/**
* Returns the client request id
*
* @return string|null
*/
public function getClientRequestId(): ?string {
return $this->getProperty("client-request-id");
}

/**
* Returns the request id
*
* @return string|null
*/
public function getRequestId(): ?string {
return $this->getProperty("request-id");
}

/**
* Returns error as a string
*
* @return string
*/
public function __toString(): string {
$errorString = ($this->getDate()) ? "Date: ".$this->getDate() : "";
$errorString .= ($this->getClientRequestId()) ? "\nClient Request Id: ".$this->getClientRequestId() : "";
$errorString .= ($this->getRequestId()) ? "\nRequest Id: ".$this->getRequestId() : "";
$errorString .= ($this->getCode()) ? "\nCode: ".$this->getCode() : "";
$errorString .= ($this->getMessage()) ? "\nMessage: ".$this->getMessage() : "";
$errorString .= ($this->getTarget()) ? "\nTarget: ".$this->getTarget() : "";
$errorString .= ($this->getDetails()) ? "\nDetails: ".$this->getDetails() : "";
$errorString .= ($this->getInnerError()) ? "\nInner Error: ".$this->getInnerError() : "";
return $errorString;
}
}
51 changes: 0 additions & 51 deletions src/Exception/GraphException.php

This file was deleted.

147 changes: 147 additions & 0 deletions src/Exception/GraphResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/


namespace Microsoft\Graph\Exception;

use Microsoft\Graph\Http\GraphRequest;

/**
* Class GraphServiceException
*
* Thrown when the Graph API returns 4xx/5xx responses
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class GraphResponseException extends \Exception
{
/**
* Headers returned in the response
*
* @var array
*/
private $responseHeaders;
/**
* HTTP response code
*
* @var int
*/
private $responseStatusCode;
/**
* JSON-decoded response body
*
* @var array
*/
private $responseBody;
/**
* The request that triggered the error response
*
* @var GraphRequest
*/
private $graphRequest;


/**
* GraphServiceException constructor.
* @param GraphRequest $graphRequest
* @param int $responseStatusCode
* @param array $responseBody
* @param array $responseHeaders
*/
public function __construct(
GraphRequest $graphRequest,
int $responseStatusCode,
array $responseBody,
array $responseHeaders
) {
$this->graphRequest = $graphRequest;
$this->responseStatusCode = $responseStatusCode;
$this->responseBody = $responseBody;
$this->responseHeaders = $responseHeaders;
$message = "'".$graphRequest->getRequestType()."' request to ".$graphRequest->getRequestUri()." returned ".$responseStatusCode."\n".json_encode($responseBody);
parent::__construct($message, $responseStatusCode);
}

/**
* Returns HTTP headers in the response from the Graph
*
* @return array<string, string[]>
*/
public function getResponseHeaders(): array {
return $this->responseHeaders;
}

/**
* Returns HTTP status code returned int the response from the Graph
*
* @return int
*/
public function getResponseStatusCode(): int {
return $this->responseStatusCode;
}

/**
* Get JSON-decoded response payload from the Graph
*
* @return array
*/
public function getRawResponseBody(): array {
return $this->responseBody;
}

/**
* Returns the error object of the payload
*
* @return ODataErrorContent|null
*/
public function getError(): ?ODataErrorContent {
if (array_key_exists("error", $this->responseBody)) {
return new ODataErrorContent($this->responseBody["error"]);
}
return null;
}

/**
* Returns the request that triggered the error response
*
* @return GraphRequest
*/
public function getRequest(): GraphRequest {
return $this->graphRequest;
}

/**
* Returns the client request Id
*
* @return string|null
*/
public function getClientRequestId(): ?string {
$headerName = "client-request-id";
if (array_key_exists($headerName, $this->responseHeaders)
&& !empty($this->responseHeaders[$headerName])) {
return $this->responseHeaders[$headerName][0];
}
return null;
}

/**
* Returns the request Id
*
* @return string|null
*/
public function getRequestId(): ?string {
$headerName = "request-id";
if (array_key_exists($headerName, $this->responseHeaders)
&& !empty($this->responseHeaders[$headerName])) {
return $this->responseHeaders[$headerName][0];
}
return null;
}
}
24 changes: 24 additions & 0 deletions src/Exception/GraphServiceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/


namespace Microsoft\Graph\Exception;

/**
* Class GraphServiceException
*
* Thrown when the Graph API returns a 4xx response
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class GraphServiceException extends GraphResponseException
{

}
Loading