MediaController: Handle errors when trying to save file

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-17 18:51:29 +02:00
parent 8e662167dc
commit 4cd80a3212
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -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);
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;
}
}
}