Skip to content

Commit d2a1c71

Browse files
beqqrry-awsford-at-aws
authored andcommitted
Adds exception handling and docblocks for the EC2 Service.
1 parent 417118d commit d2a1c71

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed

php/example_code/ec2/EC2Service.php

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
// SPDX-License-Identifier: Apache-2.0
44

5+
// snippet-start:[php.example_code.ec2.service.Ec2Service]
6+
57
namespace Ec2;
68

79
use Aws\Ec2\Ec2Client;
@@ -46,54 +48,91 @@ public function __construct($client = null, $region = 'us-west-2', $version = 'l
4648
*/
4749
public function createVpcEndpoint(string $serviceName, string $vpcId, array $routeTableIds): array
4850
{
49-
$result = $this->ec2Client->createVpcEndpoint([
50-
'ServiceName' => $serviceName,
51-
'VpcId' => $vpcId,
52-
'RouteTableIds' => $routeTableIds,
53-
]);
54-
55-
return $result["VpcEndpoint"];
51+
try {
52+
$result = $this->ec2Client->createVpcEndpoint([
53+
'ServiceName' => $serviceName,
54+
'VpcId' => $vpcId,
55+
'RouteTableIds' => $routeTableIds,
56+
]);
57+
58+
return $result["VpcEndpoint"];
59+
} catch(Ec2Exception $caught){
60+
echo "There was a problem creating the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
61+
throw $caught;
62+
}
5663
}
5764

5865
// snippet-end:[php.example_code.ec2.service.createVpcEndpoint]
5966

6067
// snippet-start:[php.example_code.ec2.service.createVpc]
6168

62-
public function createVpc($cidr)
69+
/**
70+
* @param string $cidr
71+
* @return array
72+
*/
73+
public function createVpc(string $cidr): array
6374
{
64-
$result = $this->ec2Client->createVpc([
65-
"CidrBlock" => $cidr,
66-
]);
67-
return $result['Vpc'];
75+
try {
76+
$result = $this->ec2Client->createVpc([
77+
"CidrBlock" => $cidr,
78+
]);
79+
return $result['Vpc'];
80+
}catch(Ec2Exception $caught){
81+
echo "There was a problem creating the VPC: {$caught->getAwsErrorMessage()}\n";
82+
throw $caught;
83+
}
6884
}
6985

7086
// snippet-end:[php.example_code.ec2.service.createVpc]
7187

7288
// snippet-start:[php.example_code.ec2.service.deleteVpc]
7389

74-
public function deleteVpc(mixed $vpcId)
90+
/**
91+
* @param string $vpcId
92+
* @return void
93+
*/
94+
public function deleteVpc(string $vpcId)
7595
{
76-
$result = $this->ec2Client->deleteVpc([
77-
"VpcId" => $vpcId,
78-
]);
96+
try {
97+
$this->ec2Client->deleteVpc([
98+
"VpcId" => $vpcId,
99+
]);
100+
}catch(Ec2Exception $caught){
101+
echo "There was a problem deleting the VPC: {$caught->getAwsErrorMessage()}\n";
102+
throw $caught;
103+
}
79104
}
80105

81106
// snippet-end:[php.example_code.ec2.service.deleteVpc]
82107

83108
// snippet-start:[php.example_code.ec2.service.deleteVpcEndpoint]
84109

85-
public function deleteVpcEndpoint(mixed $vpcEndpointId)
110+
/**
111+
* @param string $vpcEndpointId
112+
* @return void
113+
*/
114+
public function deleteVpcEndpoint(string $vpcEndpointId)
86115
{
87-
$result = $this->ec2Client->deleteVpcEndpoints([
88-
"VpcEndpointIds" => [$vpcEndpointId],
89-
]);
116+
try {
117+
$this->ec2Client->deleteVpcEndpoints([
118+
"VpcEndpointIds" => [$vpcEndpointId],
119+
]);
120+
}catch (Ec2Exception $caught){
121+
echo "There was a problem deleting the VPC Endpoint: {$caught->getAwsErrorMessage()}\n";
122+
throw $caught;
123+
}
90124
}
91125

92126
// snippet-end:[php.example_code.ec2.service.deleteVpcEndpoint]
93127

94128
// snippet-start:[php.example_code.ec2.service.describeRouteTables]
95129

96-
public function describeRouteTables(array $routeTableIds = [], array $filters = [])
130+
/**
131+
* @param array $routeTableIds
132+
* @param array $filters
133+
* @return array
134+
*/
135+
public function describeRouteTables(array $routeTableIds = [], array $filters = []): array
97136
{
98137
$parameters = [];
99138
if($routeTableIds){
@@ -121,17 +160,26 @@ public function describeRouteTables(array $routeTableIds = [], array $filters =
121160

122161
// snippet-start:[php.example_code.ec2.service.waitForVpcAvailable]
123162

124-
public function waitForVpcAvailable(string $VpcId)
163+
/**
164+
* @param string $VpcId
165+
* @return void
166+
*/
167+
public function waitForVpcAvailable(string $VpcId): void
125168
{
126-
127-
$waiter = $this->ec2Client->getWaiter("VpcAvailable", [
128-
'VpcIds' => [$VpcId],
129-
]);
130-
131-
$promise = $waiter->promise();
132-
$promise->wait();
133-
169+
try {
170+
$waiter = $this->ec2Client->getWaiter("VpcAvailable", [
171+
'VpcIds' => [$VpcId],
172+
]);
173+
174+
$promise = $waiter->promise();
175+
$promise->wait();
176+
}catch(Ec2Exception $caught){
177+
echo "There was a problem waiting for the VPC: {$caught->getAwsErrorMessage()}\n";
178+
throw $caught;
179+
}
134180
}
135181

136182
// snippet-end:[php.example_code.ec2.service.waitForVpcAvailable]
137183
}
184+
185+
// snippet-end:[php.example_code.ec2.service.Ec2Service]

php/example_code/ec2/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `php`
3333

3434
Code excerpts that show you how to call individual service functions.
3535

36-
- [CreateVpc](EC2Service.php#L60)
37-
- [CreateVpcEndpoint](EC2Service.php#L39)
38-
- [DeleteVpc](EC2Service.php#L72)
39-
- [DeleteVpcEndpoint](EC2Service.php#L83)
40-
- [DescribeRouteTables](EC2Service.php#L94)
36+
- [CreateVpc](EC2Service.php#L67)
37+
- [CreateVpcEndpoint](EC2Service.php#L41)
38+
- [DeleteVpc](EC2Service.php#L88)
39+
- [DeleteVpcEndpoint](EC2Service.php#L108)
40+
- [DescribeRouteTables](EC2Service.php#L128)
4141

4242

4343
<!--custom.examples.start-->

0 commit comments

Comments
 (0)