From 010b0531a5e5dac88554713196d25f867538b28d Mon Sep 17 00:00:00 2001 From: YvesMerl Date: Fri, 30 Sep 2016 12:46:20 +0200 Subject: [PATCH] chore(s3): Better S3 error handling --- package.json | 1 + packages/poosh-plugin-s3/src/RemoteClient.js | 11 +++++--- packages/poosh-plugin-s3/src/S3Error.js | 28 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 packages/poosh-plugin-s3/src/S3Error.js diff --git a/package.json b/package.json index 0767384..19f4dd8 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "scripts": { "bootstrap": "npm install && lerna bootstrap", "build": "./scripts/build.sh sourceonly", + "build:sourcemaps": "./scripts/build.sh sourcemaps", "clean": "npm run clean:build && npm run clean:test && npm run clean:npm", "clean:build": "./scripts/clean-build.sh", "clean:npm": "./scripts/clean-npm.sh", diff --git a/packages/poosh-plugin-s3/src/RemoteClient.js b/packages/poosh-plugin-s3/src/RemoteClient.js index 9a93a96..4b56f92 100644 --- a/packages/poosh-plugin-s3/src/RemoteClient.js +++ b/packages/poosh-plugin-s3/src/RemoteClient.js @@ -4,6 +4,7 @@ import joinUrl from "poosh-common/lib/url/join"; import RemoteStatus from "poosh-common/lib/file/RemoteStatus"; import normalizeFileOptions from "./options/normalizeFileOptions"; import S3 from "./promisification/S3"; +import S3Error from "./S3Error"; import S3ParamsProvider from "./S3ParamsProvider"; import { etagToMd5 } from "./helpers/convertion"; import { getStatusDetails, getStatusDetailsMissing } from "./helpers/status"; @@ -73,13 +74,12 @@ export default class RemoteClient { async getStatus(file: Object): Object { let params = this._paramsProvider.getHeadObjectParams(file); - let result; try { result = await this._s3.headObjectAsync(params); } catch (error) { if (error.code !== "NotFound") { - throw error; + throw new S3Error(error); } return { @@ -132,7 +132,12 @@ export default class RemoteClient { let params = this._paramsProvider.getListObjectsParams(this._options); params.Marker = nextMarker; - let data = await this._s3.listObjectsAsync(params); + let data; + try { + data = await this._s3.listObjectsAsync(params); + } catch (error) { + throw new S3Error(error); + } for (let content of data.Contents) { let file = this::listItemToFile(content); diff --git a/packages/poosh-plugin-s3/src/S3Error.js b/packages/poosh-plugin-s3/src/S3Error.js new file mode 100644 index 0000000..b5f5090 --- /dev/null +++ b/packages/poosh-plugin-s3/src/S3Error.js @@ -0,0 +1,28 @@ +function isForbidden(inner) { + return inner && (inner.code === "Forbidden" || inner.statusCode === 403); +} + +export default class S3Error extends Error { + + /** + * @param {Object} inner AWS inner error + */ + constructor(inner) { + + let subMessage = inner.message; + if (!subMessage && isForbidden(inner)) { + subMessage = "No access (make sure you have access to S3 ressource, and have valid policies)"; + } + + let message = `S3 ${inner.code} (${inner.statusCode})`; + if (subMessage) { + message = `${message}: ${subMessage}`; + } + + super(message); + Error.captureStackTrace(this, this.constructor); + this.name = this.constructor.name; + this.inner = inner; + } + +}