mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 17:56:30 -05:00
HistoryService: Throw NotInDBError on empty DB result
This adds error handling to getEntryByNote, so it throws a NotInDBError instead of (illegally, according to the type) returning null. Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
1a72e3c727
commit
8357f3072c
1 changed files with 16 additions and 16 deletions
|
@ -69,13 +69,19 @@ export class HistoryService {
|
||||||
* @return {HistoryEntry} the requested history entry
|
* @return {HistoryEntry} the requested history entry
|
||||||
*/
|
*/
|
||||||
private async getEntryByNote(note: Note, user: User): Promise<HistoryEntry> {
|
private async getEntryByNote(note: Note, user: User): Promise<HistoryEntry> {
|
||||||
return await this.historyEntryRepository.findOne({
|
const entry = await this.historyEntryRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
note: note,
|
note: note,
|
||||||
user: user,
|
user: user,
|
||||||
},
|
},
|
||||||
relations: ['note', 'user'],
|
relations: ['note', 'user'],
|
||||||
});
|
});
|
||||||
|
if (!entry) {
|
||||||
|
throw new NotInDBError(
|
||||||
|
`User '${user.userName}' has no HistoryEntry for Note with id '${note.id}'`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -89,13 +95,17 @@ export class HistoryService {
|
||||||
note: Note,
|
note: Note,
|
||||||
user: User,
|
user: User,
|
||||||
): Promise<HistoryEntry> {
|
): Promise<HistoryEntry> {
|
||||||
let entry = await this.getEntryByNote(note, user);
|
try {
|
||||||
if (!entry) {
|
const entry = await this.getEntryByNote(note, user);
|
||||||
entry = HistoryEntry.create(user, note);
|
|
||||||
} else {
|
|
||||||
entry.updatedAt = new Date();
|
entry.updatedAt = new Date();
|
||||||
|
return await this.historyEntryRepository.save(entry);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof NotInDBError) {
|
||||||
|
const entry = HistoryEntry.create(user, note);
|
||||||
|
return await this.historyEntryRepository.save(entry);
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
return await this.historyEntryRepository.save(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -112,11 +122,6 @@ export class HistoryService {
|
||||||
updateDto: HistoryEntryUpdateDto,
|
updateDto: HistoryEntryUpdateDto,
|
||||||
): Promise<HistoryEntry> {
|
): Promise<HistoryEntry> {
|
||||||
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
||||||
if (!entry) {
|
|
||||||
throw new NotInDBError(
|
|
||||||
`User '${user.userName}' has no HistoryEntry for Note with id '${noteIdOrAlias}'`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
entry.pinStatus = updateDto.pinStatus;
|
entry.pinStatus = updateDto.pinStatus;
|
||||||
return await this.historyEntryRepository.save(entry);
|
return await this.historyEntryRepository.save(entry);
|
||||||
}
|
}
|
||||||
|
@ -130,11 +135,6 @@ export class HistoryService {
|
||||||
*/
|
*/
|
||||||
async deleteHistoryEntry(noteIdOrAlias: string, user: User): Promise<void> {
|
async deleteHistoryEntry(noteIdOrAlias: string, user: User): Promise<void> {
|
||||||
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
const entry = await this.getEntryByNoteIdOrAlias(noteIdOrAlias, user);
|
||||||
if (!entry) {
|
|
||||||
throw new NotInDBError(
|
|
||||||
`User '${user.userName}' has no HistoryEntry for Note with id '${noteIdOrAlias}'`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
await this.historyEntryRepository.remove(entry);
|
await this.historyEntryRepository.remove(entry);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue