-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from codex-team/feat/domain-error-handler
Feat: domain error handler
- Loading branch information
Showing
10 changed files
with
108 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* DomainError entity — manually thrown exception provided by business logic | ||
*/ | ||
export class DomainError extends Error { | ||
/** | ||
* @param message - description of why the error was thrown | ||
*/ | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import type { FastifyReply } from 'fastify'; | ||
|
||
/** | ||
* Custom method for replying with information that business logic dismissed the request for some reason | ||
* | ||
* Send this error when a domain-level error is thrown | ||
* | ||
* @example | ||
* | ||
* try { | ||
* service.someAction() | ||
* } catch (error: Error) { | ||
* if (error instanceof DomainError) { | ||
* reply.domainError(error.message); | ||
* return; | ||
* } | ||
* throw error; | ||
* } | ||
* | ||
* @param message - Optional message to send. If not specified, default message will be sent | ||
*/ | ||
export default async function domainError(this: FastifyReply, message = 'Domain level error'): Promise<void> { | ||
await this | ||
.code(StatusCodes.BAD_REQUEST) | ||
.type('application/json') | ||
.send({ | ||
message, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters