Skip to content

Commit

Permalink
additional error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ricsam committed Apr 19, 2020
1 parent af259f8 commit 89998f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
12 changes: 10 additions & 2 deletions src/expressMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const expressMiddleware = (
res.setHeader('Allow', 'GET, HEAD');
res.setHeader('Content-Length', '0');
res.end();
next();
return;
}
const fpath = parse(req.url as string).pathname;
Expand All @@ -26,8 +27,15 @@ export const expressMiddleware = (
}

const stream = createReadStream(fpath);
stream.on('error', function error(err) {
next(err);
stream.once('error', function error(err) {
// next(err);
if (err.name === 'NoSuchKey') {
(res as any).statusCode = 404;
res.write('Not found');
res.end();
} else {
next(err);
}
});
stream.pipe(res);
};
Expand Down
17 changes: 6 additions & 11 deletions src/nafs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,12 @@ const activeStorageS3Serve: NAFSFactory = (url) => {
path.join(rootPath, fpath).replace(/^\//, '');

const readRemoteStream = (fpath: string) => {
try {
return s3
.getObject({
Bucket: bucket,
Key: getS3Path(fpath),
})
.createReadStream();
} catch (err) {
console.log('Yes');
}
return new stream.PassThrough();
return s3
.getObject({
Bucket: bucket,
Key: getS3Path(fpath),
})
.createReadStream();
};

let cachePath: string | undefined;
Expand Down

0 comments on commit 89998f2

Please sign in to comment.