-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-exceptions.php
82 lines (79 loc) · 2.6 KB
/
no-exceptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* @file
* Show examples of a valid response 200.
*/
require_once __DIR__ . "/vendor/autoload.php";
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$url = "https://data.dx.oregonstate.edu";
// Creating logger.
$logger = new Logger('guzzle-exception-example');
// Creating logger handlers;
$stream = new StreamHandler(__DIR__ . '/guzzle-exception-example.log', Logger::DEBUG);
$firephp = new FirePHPHandler();
// Registering handlers to logger stack.
$logger->pushHandler($stream);
$logger->pushHandler($firephp);
// Base url for API testing.
$client = new Client(['base_uri' => $url]);
try {
$response = $client->get('/jsonapi/node/services', [
'query' => [
'filter' => [
'status' => 1,
],
'fields' => [
'node--services' => 'id,title,field_exclude_trending,field_icon_name,field_service_category,field_affiliation,field_audience,field_service_synonyms,field_service_url,field_locations,field_it_system',
'taxonomy_term--categories' => 'name',
'taxonomy_term--audience' => 'name',
'taxonomy_term--affiliation' => 'name',
'taxonomy_term--locations' => 'name',
'taxonomy_term--it_systems' => 'name',
],
'include' => 'field_affiliation,field_audience,field_service_category,field_locations,field_it_system',
'sort' => 'title',
'page' => [
'limit' => 50,
'offset' => 0,
],
],
]);
}
catch (ConnectException $connectException) {
// Catch the specific connect exception to log it differently.
$logger->alert($connectException->getMessage());
}
catch (ClientException $clientException) {
// catches client only exceptions, like 404's
$logger->warning($clientException->getMessage());
}
catch (ServerException $serverException) {
// Catches 500 errors specifically.
$logger->error($serverException->getMessage());
}
catch (Exception $exception) {
// Catchall if we didn't want to handle exceptions differently.
// output to console for run.
dump($exception->getMessage());
dump($exception);
// proper logging.
$logger->warning($exception->getMessage());
throw $exception;
}
// Check if we got a response and it as 200.
if (isset($response) && $response->getStatusCode() === 200) {
$responseBody = json_decode($response->getBody(), TRUE);
dump($responseBody);
return;
}
// Dealt with all possible, problems, but didn't get back a good response.
else {
dump("Didn't get a good response.");
return;
}