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

Return 404 for non-existent file #183

Merged
merged 4 commits into from
Mar 18, 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
23 changes: 22 additions & 1 deletion src/lambda-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => {
delete args.presign;
}

let s3_response = await getS3File( config, key, args );
let s3_response;

try {
s3_response = await getS3File( config, key, args );
} catch ( e: any ) {
// An AccessDenied error means the file is either protected, or doesn't exist.
if ( e.Code === 'AccessDenied' ) {
const metadata = {
statusCode: 404,
headers: {
'Content-Type': 'text/html',
},
};
response = awslambda.HttpResponseStream.from( response, metadata );
response.write( 'File not found.' );
response.end();
return;
}
throw e;
}

if ( ! s3_response.Body ) {
throw new Error( 'No body in file.' );
}

let buffer = Buffer.from( await s3_response.Body.transformToByteArray() );

let { info, data } = await resizeBuffer( buffer, args );
Expand Down
7 changes: 3 additions & 4 deletions tests/test-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ test( 'Test content type headers', async () => {
expect( testResponseStream.contentType ).toBe( 'image/gif' );
} );

// Currently the handler will throw an error if the file is not found, rather than correctly
// return a status code and message.
test.failing( 'Test image not found', async () => {
test( 'Test image not found', async () => {
const testResponseStream = new TestResponseStream();
animatedGifLambdaEvent.rawPath = '/tachyon/does-not-exist.gif';

await handler( animatedGifLambdaEvent, testResponseStream );

expect( testResponseStream.contentType ).toBe( 'image/gif' );
jerico marked this conversation as resolved.
Show resolved Hide resolved
expect( testResponseStream.metadata.statusCode ).toBe( 404 );
expect( testResponseStream.contentType ).toBe( 'text/html' );
} );

/**
Expand Down
Loading