From e53805625256ce158197d5d326244eda9a1ff33c Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 20 Feb 2021 16:11:51 +0100 Subject: [PATCH] NotesController: Handle new errors Handle the AlreadyInDB and PermissionsUpdateInconsistent errors and correctly show them to the api user as BadRequest errors. Signed-off-by: Philip Molares --- src/api/public/notes/notes.controller.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 923cf6354..b32538d9d 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -5,6 +5,7 @@ */ import { + BadRequestException, Body, Controller, Delete, @@ -18,7 +19,11 @@ import { UnauthorizedException, UseGuards, } from '@nestjs/common'; -import { NotInDBError } from '../../../errors/errors'; +import { + AlreadyInDBError, + NotInDBError, + PermissionsUpdateInconsistentError, +} from '../../../errors/errors'; import { ConsoleLoggerService } from '../../../logger/console-logger.service'; import { NotePermissionsDto, @@ -100,9 +105,16 @@ export class NotesController { throw new UnauthorizedException('Creating note denied!'); } this.logger.debug('Got raw markdown:\n' + text, 'createNamedNote'); - return this.noteService.toNoteDto( - await this.noteService.createNote(text, noteAlias, req.user), - ); + try { + return this.noteService.toNoteDto( + await this.noteService.createNote(text, noteAlias, req.user), + ); + } catch (e) { + if (e instanceof AlreadyInDBError) { + throw new BadRequestException(e.message); + } + throw e; + } } @UseGuards(TokenAuthGuard) @@ -191,6 +203,9 @@ export class NotesController { if (e instanceof NotInDBError) { throw new NotFoundException(e.message); } + if (e instanceof PermissionsUpdateInconsistentError) { + throw new BadRequestException(e.message); + } throw e; } }