NotesService: Throw NotInDBError when the note wasn't found

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-17 18:47:10 +02:00
parent dea3c1d393
commit ec8cf6d33e
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 25 additions and 4 deletions

7
src/errors/errors.ts Normal file
View file

@ -0,0 +1,7 @@
export class NotInDBError extends Error {
name = 'NotInDBError';
}
export class ClientError extends Error {
name = 'ClientError';
}

View file

@ -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<Note> {
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;
}

View file

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