forked from aws/aws-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: Add handling specific service Error code (aws#862)
Adds example which shows how specific service error codes can be handled.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Handling Specific Service Error Codes | ||
|
||
This examples highlights how you can use the `awserr.Error` type to perform logic based on specific error codes returned by service API operations. | ||
|
||
In this example the `S3` `GetObject` API operation is used to request the contents of a object in S3. The example handles the `NoSuchBucket` and `NoSuchKey` error codes printing custom messages to stderr. If Any other error is received a generic message is printed. | ||
|
||
## Usage | ||
|
||
Will make a request to S3 for the contents of an object. If the request was successful, and the object was found the object's path and size will be printed to stdout. | ||
|
||
If the object's bucket or key does not exist a specific error message will be printed to stderr for the error. | ||
|
||
Any other error will be printed as an unknown error. | ||
|
||
```sh | ||
go run handleServiceErrorCodes.go mybucket mykey | ||
``` |
67 changes: 67 additions & 0 deletions
67
example/aws/request/handleServiceErrorCodes/handleServiceErrorCodes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
) | ||
|
||
func exitErrorf(msg string, args ...interface{}) { | ||
fmt.Fprintf(os.Stderr, msg+"\n", args...) | ||
os.Exit(1) | ||
} | ||
|
||
// Will make a request to S3 for the contents of an object. If the request | ||
// was successful, and the object was found the object's path and size will be | ||
// printed to stdout. | ||
// | ||
// If the object's bucket or key does not exist a specific error message will | ||
// be printed to stderr for the error. | ||
// | ||
// Any other error will be printed as an unknown error. | ||
// | ||
// Usage: handleServiceErrorCodes <bucket> <key> | ||
func main() { | ||
if len(os.Args) < 3 { | ||
exitErrorf("Usage: %s <bucket> <key>", filepath.Base(os.Args[0])) | ||
} | ||
sess, err := session.NewSession() | ||
if err != nil { | ||
exitErrorf("failed to create session,", err) | ||
} | ||
|
||
svc := s3.New(sess) | ||
resp, err := svc.GetObject(&s3.GetObjectInput{ | ||
Bucket: aws.String(os.Args[1]), | ||
Key: aws.String(os.Args[2]), | ||
}) | ||
|
||
if err != nil { | ||
// Casting to the awserr.Error type will allow you to inspect the error | ||
// code returned by the service in code. The error code can be used | ||
// to switch on context specific functionality. In this case a context | ||
// specific error message is printed to the user based on the bucket | ||
// and key existing. | ||
// | ||
// For information on other S3 API error codes see: | ||
// http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case "NoSuchBucket": | ||
exitErrorf("bucket %s does not exist", os.Args[1]) | ||
case "NoSuchKey": | ||
exitErrorf("object with key %s does not exist in bucket %s", os.Args[2], os.Args[1]) | ||
} | ||
} | ||
exitErrorf("unknown error occured, %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
fmt.Printf("s3://%s/%s exists. size: %d\n", os.Args[1], os.Args[2], | ||
aws.Int64Value(resp.ContentLength)) | ||
} |