

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS SDK for PHP 버전 3의 오류 처리
<a name="handling-errors"></a>

## 동기 오류 처리
<a name="synchronous-error-handling"></a>

작업을 수행하는 동안 오류가 발생할 경우 예외가 발생합니다. 이러한 이유로 코드에서 오류를 처리해야 하는 경우 작업 둘레에 `try`/`catch` 블록을 사용해야 합니다. 오류가 발생하면 SDK는 서비스별 예외를 발생시킵니다.

다음 예제에서는 `Aws\S3\S3Client`를 사용합니다. 오류가 있는 경우 발생한 예외는 `Aws\S3\Exception\S3Exception` 유형입니다. SDK가 발생시키는 모든 서비스별 예외는 `Aws\Exception\AwsException` 클래스에서 확장됩니다. 이 클래스에는 request-id, 오류 코드 및 오류 유형을 포함하여 실패에 대한 유용한 정보가 포함되어 있습니다. 이를 지원하는 몇 가지 서비스를 참고하고, 응답 데이터는 결합형 배열 구조(`Aws\Result` 객체와 유사함)로 강제 변환되며, 이는 일반 PHP 결합형 배열처럼 액세스할 수 있습니다. `toArray()` 메서드는 존재할 경우 어떤 데이터이든 반환합니다.

 **가져오기** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **샘플 코드** 

```
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk([
    'region'   => 'us-west-2'
]);

// Use an Aws\Sdk class to create the S3Client object.
$s3Client = $sdk->createS3();

try {
    $s3Client->createBucket(['Bucket' => 'amzn-s3-demo-bucket']);
} catch (S3Exception $e) {
    // Catch an S3 specific exception.
    echo $e->getMessage();
} catch (AwsException $e) {
    // This catches the more generic AwsException. You can grab information
    // from the exception using methods of the exception object.
    echo $e->getAwsRequestId() . "\n";
    echo $e->getAwsErrorType() . "\n";
    echo $e->getAwsErrorCode() . "\n";

    // This dumps any modeled response data, if supported by the service
    // Specific members can be accessed directly (e.g. $e['MemberName'])
    var_dump($e->toArray());
}
```

## 비동기 오류 처리
<a name="asynchronous-error-handling"></a>

비동기 요청을 전송할 때는 예외가 발생하지 않습니다. 그 대신, 반환된 promise의 `then()` 또는 `otherwise()` 메서드를 사용하여 결과 또는 오류를 수신해야 합니다.

 **가져오기** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **샘플 코드** 

```
//Asynchronous Error Handling
$promise = $s3Client->createBucketAsync(['Bucket' => 'amzn-s3-demo-bucket']);
$promise->otherwise(function ($reason) {
    var_dump($reason);
});

// This does the same thing as the "otherwise" function.
$promise->then(null, function ($reason) {
    var_dump($reason);
});
```

그 대신 promise를 "언래핑"하고 예외를 발생시킬 수 있습니다.

 **가져오기** 

```
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Aws\S3\Exception\S3Exception;
```

 **샘플 코드** 

```
$promise = $s3Client->createBucketAsync(['Bucket' => 'amzn-s3-demo-bucket']);
```

```
//throw exception
try {
    $result = $promise->wait();
} catch (S3Exception $e) {
    echo $e->getMessage();
}
```