Skip to content

Commit

Permalink
Now we have DomainError entity
Browse files Browse the repository at this point in the history
  • Loading branch information
elizachi committed Jan 13, 2024
1 parent c025e46 commit 38f2c23
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/domain/entities/domainError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* DomainError entity
*/
export class DomainError extends Error {
/**
* @param message - description of why the error was thrown
*/
constructor(message: string) {
super(message);
}
}
7 changes: 4 additions & 3 deletions src/domain/service/note.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Note, NoteInternalId, NotePublicId } from '@domain/entities/note.js';
import type NoteRepository from '@repository/note.repository.js';
import { createPublicId } from '@infrastructure/utils/id.js';
import { DomainError } from '@domain/entities/domainError';

/**
* Note service
Expand Down Expand Up @@ -54,7 +55,7 @@ export default class NoteService {
const updatedNote = await this.repository.updateNoteContentById(id, content);

if (updatedNote === null) {
throw new Error(`Note with id ${id} was not updated`);
throw new DomainError(`Note with id ${id} was not updated`);
}

return updatedNote;
Expand All @@ -69,7 +70,7 @@ export default class NoteService {
const note = await this.repository.getNoteById(id);

if (note === null) {
throw new Error(`Note with id ${id} was not found`);
throw new DomainError(`Note with id ${id} was not found`);
}

return note;
Expand All @@ -84,7 +85,7 @@ export default class NoteService {
const note = await this.repository.getNoteByPublicId(publicId);

if (note === null) {
throw new Error(`Note with public id ${publicId} was not found`);
throw new DomainError(`Note with public id ${publicId} was not found`);
}

return note;
Expand Down
9 changes: 5 additions & 4 deletions src/presentation/http/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import NoteListRouter from '@presentation/http/router/noteList.js';
import { EditorToolSchema } from './schema/EditorTool.js';
import JoinRouter from '@presentation/http/router/join.js';
import { JoinSchemaParams, JoinSchemaResponse } from './schema/Join.js';
import { DomainError } from '@domain/entities/domainError.js';


const appServerLogger = getLogger('appServer');
Expand Down Expand Up @@ -338,10 +339,10 @@ export default class HttpApi implements Api {
*/
private domainErrorHandler(): void {
this.server?.setErrorHandler(function (error, request, reply) {
const statusCode = error.statusCode;

// eslint-disable-next-line @typescript-eslint/no-magic-numbers
if (statusCode === 500) {
/**
* If we have an error that occurs in the domain-level we reply it with special format
*/
if (error instanceof DomainError) {
this.log.error(error);
void reply.domainError(error.message);
}
Expand Down

0 comments on commit 38f2c23

Please sign in to comment.