-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend | InternalCommandSender exception handling
- Loading branch information
1 parent
30b277d
commit def8863
Showing
19 changed files
with
124 additions
and
14 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,15 @@ | ||
export class Exception extends Error { | ||
constructor(message: string, private readonly causation: Error | undefined = undefined) { | ||
super(message); | ||
this.stack = causation ? Exception.errorCausedBy(this, causation).stack : this.stack; | ||
} | ||
|
||
causedBy(causation: Error): Exception { | ||
return new Exception(Exception.errorCausedBy(this, causation).message, causation); | ||
} | ||
|
||
private static errorCausedBy(error: Error, causation: Error): Error { | ||
error.stack += '\nCaused by: \n' + causation.message + '\n' + causation.stack; | ||
return error; | ||
} | ||
} |
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 @@ | ||
export * from './exception'; |
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"outDir": "../../dist/libs/typescript-sdk" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"] | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
5 changes: 3 additions & 2 deletions
5
...ncel-applicant-invitation.request-body.ts → ...el-applicant-invitation.request-params.ts
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
15 changes: 15 additions & 0 deletions
15
...ite-side/application/internal-command-sender/internal-command-invalid-schema.exception.ts
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,15 @@ | ||
import { InternalCommand } from './internal-command'; | ||
import { Exception } from '@coders-board-library/typescript-sdk/exception'; | ||
import { ValidationError } from 'class-validator'; | ||
|
||
export class InternalCommandInvalidSchemaException extends Exception { | ||
constructor(command: InternalCommand, readonly validationErrors: ValidationError[]) { | ||
super( | ||
`Internal command ${ | ||
command.constructor.name | ||
} rejected! Schema doesn't match. \n Validation errors: ${validationErrors.map( | ||
(it, index) => `\n${index + 1}. ${it}`, | ||
)}`, | ||
); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...nel/write-side/application/internal-command-sender/internal-command-rejected.exception.ts
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,8 @@ | ||
import { InternalCommand } from './internal-command'; | ||
import { Exception } from '@coders-board-library/typescript-sdk/exception'; | ||
|
||
export class InternalCommandRejectedException extends Exception { | ||
constructor(command: InternalCommand, causation: Error | undefined) { | ||
super(`Internal command ${command.constructor.name} rejected!`, causation); | ||
} | ||
} |
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
16 changes: 13 additions & 3 deletions
16
...rnel/write-side/infrastructure/internal-command-sender/class-validatior-command-sender.ts
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 |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { InternalCommandSender } from '../../application/internal-command-sender/internal-command-sender'; | ||
import { InternalCommand } from '../../application/internal-command-sender/internal-command'; | ||
import { validateOrReject } from 'class-validator'; | ||
import { InternalCommandRejectedException } from '../../application/internal-command-sender/internal-command-rejected.exception'; | ||
import { InternalCommandInvalidSchemaException } from '../../application/internal-command-sender/internal-command-invalid-schema.exception'; | ||
|
||
export class ClassValidatorInternalCommandSender implements InternalCommandSender { | ||
constructor(private readonly commandSender: InternalCommandSender) {} | ||
|
||
sendAndWait<R>(command: InternalCommand): Promise<R> { | ||
return validateOrReject(command).then(() => this.commandSender.sendAndWait<R>(command)); | ||
return validateOrReject(command) | ||
.catch(validationError => { | ||
throw new InternalCommandInvalidSchemaException(command, validationError); | ||
}) | ||
.then(() => this.commandSender.sendAndWait<R>(command)); | ||
} | ||
|
||
sendAndForget<T extends InternalCommand>(command: T) { | ||
return validateOrReject(command).then(() => this.commandSender.sendAndForget(command)); | ||
sendAndForget<T extends InternalCommand>(command: T): Promise<void> { | ||
return validateOrReject(command) | ||
.catch(validationError => { | ||
throw new InternalCommandInvalidSchemaException(command, validationError); | ||
}) | ||
.then(() => this.commandSender.sendAndForget(command)); | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
...tion/rest-api/nestjs-exception-filter/internal-command-invalid-schema.exception-filter.ts
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,18 @@ | ||
import { ExceptionFilter, Catch, ArgumentsHost, HttpStatus } from '@nestjs/common'; | ||
import { InternalCommandInvalidSchemaException } from '../../../application/internal-command-sender/internal-command-invalid-schema.exception'; | ||
|
||
@Catch(InternalCommandInvalidSchemaException) | ||
export class InternalCommandInvalidSchemaExceptionFilter implements ExceptionFilter { | ||
catch(exception: InternalCommandInvalidSchemaException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse(); | ||
const request = ctx.getRequest(); | ||
|
||
response.status(HttpStatus.BAD_REQUEST).json({ | ||
statusCode: HttpStatus.BAD_REQUEST, | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
message: exception.validationErrors, //TODO: Flatten from NestJS validation pipe! | ||
}); | ||
} | ||
} |
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