Skip to content

Commit

Permalink
Improved logging for S3 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wmaurer committed Jul 6, 2023
1 parent f2df81c commit e854df2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
1 change: 1 addition & 0 deletions apps/server-asset-sg/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class AppController {

const e = await this.appService.getFile(maybeFileId.right)();
if (E.isLeft(e)) {
console.log(JSON.stringify(e.left));
throw new HttpException(e.left.message, 500);
}
const result = e.right;
Expand Down
21 changes: 8 additions & 13 deletions apps/server-asset-sg/src/app/file/get-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as E from 'fp-ts/Either';
import { pipe } from 'fp-ts/function';
import * as TE from 'fp-ts/TaskEither';

import { unknownToError } from '@asset-sg/core';
import { TypedError, unknownErrorTag, unknownToError } from '@asset-sg/core';

import { assetFolder, bucketName, createS3Client, destroyS3Client } from './common';

Expand All @@ -14,21 +14,16 @@ export const getFile = (prismaClient: PrismaClient, fileId: number) => {
s3Client =>
pipe(
TE.tryCatch(() => pipe(prismaClient.file.findFirstOrThrow({ where: { fileId } })), unknownToError),
TE.chain(({ fileName }) =>
pipe(
TE.chainW(({ fileName }) => {
const key = (assetFolder ? assetFolder + '/' : '') + fileName;
return pipe(
TE.tryCatch(
() =>
s3Client.send(
new GetObjectCommand({
Key: (assetFolder ? assetFolder + '/' : '') + fileName,
Bucket: bucketName,
}),
),
unknownToError,
() => s3Client.send(new GetObjectCommand({ Key: key, Bucket: bucketName })),
e => new TypedError(unknownErrorTag, e, 'Unable to get file from S3 with key ' + key),
),
TE.map(a => ({ ...a, fileName })),
),
),
);
}),
TE.map(a => ({
fileName: a.fileName,
stream: a.Body as NodeJS.ReadableStream,
Expand Down

0 comments on commit e854df2

Please sign in to comment.