Skip to content

Commit

Permalink
chore(s3): Better S3 error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
yvele committed Sep 30, 2016
1 parent 8889892 commit 010b053
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 8 additions & 3 deletions packages/poosh-plugin-s3/src/RemoteClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions packages/poosh-plugin-s3/src/S3Error.js
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 010b053

Please sign in to comment.