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

Fix s3 integration error handling #15180

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 61 additions & 23 deletions packages/server/src/integrations/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,12 @@ class S3Integration implements IntegrationBase {
try {
await this.client.listBuckets().promise()
response.connected = true
} catch (e: any) {
response.error = e.message as string
} catch (e: unknown) {
if (e instanceof Error) {
response.error = e.message as string
} else {
response.error = "AWS S3: Unable to process the request"
}
}
return response
}
Expand Down Expand Up @@ -209,7 +213,20 @@ class S3Integration implements IntegrationBase {
LocationConstraint: query.location,
}
}
return await this.client.createBucket(params).promise()

try {
const response = await this.client.createBucket(params).promise()
return response
} catch (e: unknown) {
let message = "Unable to process the request"
if (e instanceof Error) {
message = e.message
}

// Do not rethrow AWS errors as the statusCode prop will be
// interpreted by koa
throw new Error("AWS S3: " + message)
}
}

async read(query: {
Expand All @@ -220,16 +237,25 @@ class S3Integration implements IntegrationBase {
maxKeys: number
prefix: string
}) {
const response = await this.client
.listObjects({
Bucket: query.bucket,
Delimiter: query.delimiter,
Marker: query.marker,
MaxKeys: query.maxKeys,
Prefix: query.prefix,
})
.promise()
return response.Contents
try {
const response = await this.client
.listObjects({
Bucket: query.bucket,
Delimiter: query.delimiter,
Marker: query.marker,
MaxKeys: query.maxKeys,
Prefix: query.prefix,
})
.promise()
return response.Contents
} catch (e: unknown) {
let message = "Unable to process the request"
if (e instanceof Error) {
message = e.message
}

throw new Error("AWS S3: " + message)
}
}

async readCsv(query: { bucket: string; key: string }) {
Expand All @@ -253,22 +279,34 @@ class S3Integration implements IntegrationBase {
stream.on("finish", () => {
resolve(response)
})
}).catch(err => {
}).catch((e: unknown) => {
let message = "Unable to process the request"
if (csvError) {
throw new Error("Could not read CSV")
} else {
throw err
message = "Could not read CSV"
} else if (e instanceof Error) {
message = e.message
}
throw new Error("AWS S3: " + message)
})
}

async delete(query: { bucket: string; delete: string }) {
return await this.client
.deleteObjects({
Bucket: query.bucket,
Delete: JSON.parse(query.delete),
})
.promise()
try {
const response = await this.client
.deleteObjects({
Bucket: query.bucket,
Delete: JSON.parse(query.delete),
})
.promise()
return response
} catch (e: unknown) {
let message = "Unable to process the request"
if (e instanceof Error) {
message = e.message
}

throw new Error("AWS S3: " + message)
}
}
}

Expand Down
Loading