From 5a07abfd43e03ba23153cb96a7080832968fe31f Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 17 Oct 2020 18:51:29 +0200 Subject: [PATCH] MediaController: Handle errors when trying to save file Signed-off-by: David Mehren --- src/api/public/media/media.controller.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/api/public/media/media.controller.ts b/src/api/public/media/media.controller.ts index ce82247dc..a2dc65ac4 100644 --- a/src/api/public/media/media.controller.ts +++ b/src/api/public/media/media.controller.ts @@ -1,4 +1,5 @@ import { + BadRequestException, Controller, Headers, Post, @@ -6,6 +7,7 @@ import { UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; +import { ClientError, NotInDBError } from '../../../errors/errors'; import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { MediaService } from '../../../media/media.service'; import { MulterFile } from '../../../media/multer-file.interface'; @@ -33,10 +35,16 @@ export class MediaController { `Recieved filename '${file.originalname}' for note '${noteId}' from user '${username}'`, 'uploadImage', ); - const note = await this.notesService.getNoteByIdOrAlias(noteId); - const url = await this.mediaService.saveFile(file, username, note.id); - return { - link: url, - }; + try { + const url = await this.mediaService.saveFile(file, username, noteId); + return { + link: url, + }; + } catch (e) { + if (e instanceof ClientError || e instanceof NotInDBError) { + throw new BadRequestException(e.message); + } + throw e; + } } }