Skip to content

Commit 557a2f4

Browse files
committed
handle error responses
1 parent bc4e26a commit 557a2f4

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

src/AdminResources/CalendarEvents.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class CalendarEvents extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -58,6 +58,11 @@ public function get(string $calendarId, array $params = []): array
5858
}
5959

6060
$events = MsGraphAdmin::get("users/$this->userId/calendars/$calendarId/events?$params");
61+
62+
if (isset($events->error)) {
63+
throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}");
64+
}
65+
6166
$data = MsGraphAdmin::getPagination($events, $top, $skip);
6267

6368
return [

src/AdminResources/Calendars.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Calendars extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$calendars = MsGraphAdmin::get("users/$this->userId/calendars?$params");
6161

62+
if (isset($calendars->error)) {
63+
throw new Exception("Graph API Error, code: {$calendars->error->code}, Message: {$calendars->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($calendars, $top, $skip);
6367

6468
return [

src/AdminResources/Contacts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Contacts extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$contacts = MsGraphAdmin::get('users/'.$this->userId.'/contacts?'.$params);
6161

62+
if (isset($contacts->error)) {
63+
throw new Exception("Graph API Error, code: {$contacts->error->code}, Message: {$contacts->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($contacts, $top, $skip);
6367

6468
return [

src/AdminResources/Emails.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Emails extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
private string $subject = '';
1717

@@ -129,9 +129,13 @@ public function get(array $params = []): array
129129
$params = http_build_query($params);
130130
}
131131

132-
//get messages from folderId
132+
// get messages from folderId
133133
$emails = MsGraphAdmin::get('users/'.$this->userId.'/messages?'.$params);
134134

135+
if (isset($emails->error)) {
136+
throw new Exception("Graph API Error, code: {$emails->error->code}, Message: {$emails->error->message}");
137+
}
138+
135139
$data = MsGraphAdmin::getPagination($emails, $top, $skip);
136140

137141
return [
@@ -163,21 +167,21 @@ public function findInlineAttachments(array $email): array
163167
{
164168
$attachments = self::findAttachments($email['id']);
165169

166-
//replace every case of <img='cid:' with the base64 image
170+
// replace every case of <img='cid:' with the base64 image
167171
$email['body']['content'] = preg_replace_callback(
168172
'~cid.*?"~',
169173
function (array $m) use ($attachments) {
170-
//remove the last quote
174+
// remove the last quote
171175
$parts = explode('"', $m[0]);
172176

173-
//remove cid:
177+
// remove cid:
174178
$contentId = str_replace('cid:', '', $parts[0]);
175179

176-
//loop over the attachments
180+
// loop over the attachments
177181
foreach ($attachments['value'] as $file) {
178-
//if there is a match
182+
// if there is a match
179183
if ($file['contentId'] == $contentId) {
180-
//return a base64 image with a quote
184+
// return a base64 image with a quote
181185
return 'data:'.$file['contentType'].';base64,'.$file['contentBytes'].'"';
182186
}
183187
}
@@ -217,7 +221,7 @@ public function send(): void
217221
/**
218222
* @throws Exception
219223
*/
220-
public function reply()
224+
public function reply(): MsGraphAdmin
221225
{
222226
if (strlen($this->userId) === 0) {
223227
throw new Exception('userId is required.');
@@ -237,7 +241,7 @@ public function reply()
237241
/**
238242
* @throws Exception
239243
*/
240-
public function forward()
244+
public function forward(): MsGraphAdmin
241245
{
242246
if (strlen($this->userId) === 0) {
243247
throw new Exception('userId is required.');
@@ -257,7 +261,7 @@ public function forward()
257261
/**
258262
* @throws Exception
259263
*/
260-
public function delete(string $id)
264+
public function delete(string $id): MsGraphAdmin
261265
{
262266
if ($this->userId == null) {
263267
throw new Exception('userId is required.');
@@ -297,12 +301,12 @@ protected function prepareEmail(): array
297301
}
298302
}
299303

300-
$attachmentarray = [];
304+
$attachmentArray = [];
301305
if ($attachments != null) {
302306
foreach ($attachments as $file) {
303307
$path = pathinfo($file);
304308

305-
$attachmentarray[] = [
309+
$attachmentArray[] = [
306310
'@odata.type' => '#microsoft.graph.fileAttachment',
307311
'name' => $path['basename'],
308312
'contentType' => mime_content_type($file),
@@ -330,8 +334,8 @@ protected function prepareEmail(): array
330334
if ($bccArray != null) {
331335
$envelope['message']['bccRecipients'] = $bccArray;
332336
}
333-
if ($attachmentarray != null) {
334-
$envelope['message']['attachments'] = $attachmentarray;
337+
if ($attachmentArray != null) {
338+
$envelope['message']['attachments'] = $attachmentArray;
335339
}
336340
if ($comment != null) {
337341
$envelope['comment'] = $comment;

src/AdminResources/Events.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Events extends MsGraphAdmin
99
{
1010
private string $userId = '';
1111

12-
private string $top = '';
12+
private string $top = '100';
1313

14-
private string $skip = '';
14+
private string $skip = '0';
1515

1616
public function userid(string $userId): static
1717
{
@@ -59,6 +59,10 @@ public function get(array $params = []): array
5959

6060
$events = MsGraphAdmin::get("users/$this->userId/events?$params");
6161

62+
if (isset($events->error)) {
63+
throw new Exception("Graph API Error, code: {$events->error->code}, Message: {$events->error->message}");
64+
}
65+
6266
$data = MsGraphAdmin::getPagination($events, $top, $skip);
6367

6468
return [

0 commit comments

Comments
 (0)