From 8b66e589d7ccb24afdf4c112257d3859b9aee2b6 Mon Sep 17 00:00:00 2001 From: Yi-Ting Shih Date: Thu, 10 Jul 2025 16:26:16 +0800 Subject: [PATCH] Fix: add not found response --- src/app.controller.ts | 3 +++ src/app.service.ts | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/app.controller.ts b/src/app.controller.ts index 5890efe..39348a0 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -45,6 +45,9 @@ export class AppController { Key: pkg, Range: range, } as GetObjectCommandInput); + if (!stream) + throw new HttpException( + `No such file '${pkg}'`, HttpStatus.NOT_FOUND); return new StreamableFile(stream); } diff --git a/src/app.service.ts b/src/app.service.ts index 7c3efeb..51b870a 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -6,6 +6,8 @@ import { S3ClientConfig, GetObjectCommand, GetObjectCommandInput, + GetObjectCommandOutput, + NoSuchKey, } from '@aws-sdk/client-s3'; @Injectable() @@ -28,14 +30,22 @@ export class AppService { return 'OK'; } - async getObject(getObjectConfig: GetObjectCommandInput): Promise { + async getObject(getObjectConfig: GetObjectCommandInput): Promise { const command = new GetObjectCommand(getObjectConfig); - const res = await this.s3Client.send(command); + let res: GetObjectCommandOutput; + try { + res = await this.s3Client.send(command); + } catch (err: unknown) { + if (err instanceof NoSuchKey) + return null; + throw err; + } if (!res.Body) throw new HttpException( 's3 get object failed', HttpStatus.INTERNAL_SERVER_ERROR, ); + return res.Body as Readable; } }