Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift: How to handle errors from the Swift SDK #6107

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions swift/example_code/swift-sdk/ErrorHandling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
34 changes: 34 additions & 0 deletions swift/example_code/swift-sdk/ErrorHandling/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to
// build this package.
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import PackageDescription

let package = Package(
name: "ErrorHandling",
// Let Xcode know the minimum Apple platforms supported.
platforms: [
.macOS(.v11),
.iOS(.v13)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/awslabs/aws-sdk-swift",
from: "0.34.0"
),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "ErrorHandling",
dependencies: [
.product(name: "AWSS3", package: "aws-sdk-swift"),
],
path: "Sources"),
]
)
64 changes: 64 additions & 0 deletions swift/example_code/swift-sdk/ErrorHandling/Sources/entry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// An example showing how to handle service and HTTP errors when using the AWS
// SDK for Swift.
import Foundation
import AwsCommonRuntimeKit
import ClientRuntime
import AWSS3

/// Main entry point.
@main
struct ErrorHandlingExample {
static func main() async {
await SDKLoggingSystem.initialize(logLevel: .error)

print("Calling ListBuckets using a Region name that doesn't exist. This")
print("should result in a DNS resolution error.")

// snippet-start:[errors.swift.service-error]
do {
let client = try S3Client(region: "un-real-1")

_ = try await client.listBuckets(input: ListBucketsInput())
print("Done")
} catch let error as CommonRunTimeError {
switch error {
case .crtError(let error):
print("Common RunTime error: (code \(error.code)) (\(error.name)): \(error.message)")
break
default:
// This should never happen in current versions of the
// SDK, but is here to future-proof this error handler.
dump(error, name: "Unknown type of CommonRunTimeError")
}
} catch let error as CRTError {
print("CRT Error (code \(error.code)) (\(error.name)): \(error.message)")
} catch {
print("Some other error")
}
// snippet-end:[errors.swift.service-error]

print("\n\n")
print("Calling GetObject with bucket and key names that likely don't")
print("exist. If that's the case, this will result in an HTTP error 403")
print("(Forbidden).")

// snippet-start:[errors.swift.http-error]
do {
let client = try S3Client(region: "us-east-1")

_ = try await client.getObject(input: GetObjectInput(
bucket: "not-a-real-bucket",
key: "not-a-real-key"
))
print("Found a matching bucket but shouldn't have!")
} catch let error as HTTPError {
print("HTTP error; status code: \(error.httpResponse.statusCode.rawValue)")
} catch {
dump(error)
}
// snippet-end:[errors.swift.http-error]
}
}
Loading