diff --git a/src/lambda-handler.ts b/src/lambda-handler.ts index 4325c41..71fa18d 100644 --- a/src/lambda-handler.ts +++ b/src/lambda-handler.ts @@ -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 ); diff --git a/tests/test-lambda.ts b/tests/test-lambda.ts index 73fa969..e5fd4d8 100644 --- a/tests/test-lambda.ts +++ b/tests/test-lambda.ts @@ -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' ); + expect( testResponseStream.metadata.statusCode ).toBe( 404 ); + expect( testResponseStream.contentType ).toBe( 'text/html' ); } ); /**