diff --git a/src/errors/errors.ts b/src/errors/errors.ts new file mode 100644 index 000000000..6e1d00da6 --- /dev/null +++ b/src/errors/errors.ts @@ -0,0 +1,7 @@ +export class NotInDBError extends Error { + name = 'NotInDBError'; +} + +export class ClientError extends Error { + name = 'ClientError'; +} diff --git a/src/notes/notes.service.ts b/src/notes/notes.service.ts index 2233fc87f..f6d5e4994 100644 --- a/src/notes/notes.service.ts +++ b/src/notes/notes.service.ts @@ -1,6 +1,7 @@ import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; +import { NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { Revision } from '../revisions/revision.entity'; import { RevisionsService } from '../revisions/revisions.service'; @@ -132,8 +133,19 @@ export class NotesService { } async getNoteByIdOrAlias(noteIdOrAlias: string): Promise { + this.logger.debug( + `Trying to find note '${noteIdOrAlias}'`, + 'getNoteByIdOrAlias', + ); const note = await this.noteRepository.findOne({ - where: [{ id: noteIdOrAlias }, { alias: noteIdOrAlias }], + where: [ + { + id: noteIdOrAlias, + }, + { + alias: noteIdOrAlias, + }, + ], relations: [ 'authorColors', 'owner', @@ -142,8 +154,9 @@ export class NotesService { ], }); if (note === undefined) { - //TODO: Improve error handling - throw new Error('Note not found'); + throw new NotInDBError( + `Note with id/alias '${noteIdOrAlias}' not found.`, + ); } return note; } diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index a27e0ef7b..09678dbb6 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -3,6 +3,7 @@ import { Test } from '@nestjs/testing'; import { TypeOrmModule } from '@nestjs/typeorm'; import * as request from 'supertest'; import { PublicApiModule } from '../../src/api/public/public-api.module'; +import { NotInDBError } from '../../src/errors/errors'; import { GroupsModule } from '../../src/groups/groups.module'; import { LoggerModule } from '../../src/logger/logger.module'; import { NotesModule } from '../../src/notes/notes.module'; @@ -82,7 +83,7 @@ describe('Notes', () => { .delete('/notes/test3') .expect(200); return expect(notesService.getNoteByIdOrAlias('test3')).rejects.toEqual( - Error('Note not found'), + new NotInDBError("Note with id/alias 'test3' not found."), ); });