
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
@betsys-nestjs/exception-handling
Advanced tools
This library is responsible for handling exception thrown in application
Library serves for handling of exceptions based on communication type (HTTP, GRPC)
| Package | Version |
|---|---|
| @grpc/grpc-js | ^1.8.0 |
| @betsys-nestjs/logger | ^5.0.0 |
| @nestjs/common | ^10.0.0 |
| @nestjs/config | ^3.0.0 |
| @nestjs/core | ^10.0.0 |
| @nestjs/microservices | ^10.0.0 |
| express | ^4.0.0 |
| reflect-metadata | ^0.1.12 |
| rxjs | ^7.1.0 |
| class-validator | ^0.13.2 |
| class-transformer | ^0.5.1 |
To start using this library simply import ExceptionHandlingModule and use it like this:
ExceptionHandlingModule has method with this signature forRoot('express' | 'grpc')
import {ExceptionHandlingModule} from '@betsys-nestjs/exception-handling'
import {Module} from "@nestjs/common";
@Module({
imports: [
ExceptionHandlingModule.forRoot('http')
]
})
class AppModule {
}
{
"errors": [
{
"code": "code",
"message": "message"
}
]
}
There are 3 different exception types that we distinguish between:
DomainException - An exception that extends BaseDomainException from this library.RequestValidationException - An exception which is mapped from the exceptions thrown by class-validator library.All other exceptions - Any other thrown exceptionsEach exception also provides automatic logging, but it's required to initialize the LoggingModule like this:
import {LoggerModule} from "@betsys-nestjs/logger";
LoggerModule.forRoot('http', [])
This bootstraps Logger library globally and is used by ExceptionHandling library automatically.
Domain exception is thrown within an application and must extend BaseDomainException.
Commonly domain exception is mapped to HttpException within an application controller,
we do it automatically via the decorator UseDomainExceptionMapper which specifies the mappings from domain exceptions
to http exceptions.
You can define any amount of exception mappings.
This decorator can be used on either the level of a controller or a specific endpoint within the controller.
import {
UseDomainExceptionMapper,
HttpExceptionInterface,
BaseDomainException
} from '@betsys-nestjs/exception-handling';
import {Controller, Get} from "@nestjs/common";
class TestDomainException extends BaseDomainException {
static create(): TestDomainException {
return new TestDomainException(`Domain exception thrown.`);
}
}
@Controller()
class TestController {
@UseDomainExceptionMapper<HttpExceptionInterface>(
'http',
new Map<string, HttpExceptionInterface>([
[
TestDomainException.name,
{
message: 'Test domain exception message',
code: 'TEST_DOMAIN_EXCEPTION',
statusCode: HttpStatus.CONFLICT,
},
],
])
)
@Get('domain-exception')
async testDomainException(): Promise<void> {
throw TestDomainException.create();
}
}
Commonly exceptions thrown by class-validator look like this:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
"email must be an email"
]
}
Using this library you will get more readable exception interface defined above.
import {IsString, IsInt} from 'class-validator';
import {Body, Controller, Get} from "@nestjs/common";
class RequestDto {
@IsString()
name!: string;
@IsInt()
age!: number;
}
@Controller('test')
export class TestController {
@Get('validation-exception')
async testValidationException(@Body() dto: RequestDto): Promise<void> {
}
}
All other native exceptions thrown within an application will are caught as logic exceptions.
They are always mapped to this HttpException with http status of 500:
{
"errors": [
{
"code": "INTERNAL_SERVER_ERROR",
"message": "Internal server error"
}
]
}
import {Controller, Get} from "@nestjs/common";
@Controller('test')
export class TestController {
@Get('logic-exception')
async testLogicException(): Promise<void> {
throw new Error('Error');
}
}
FAQs
This library is responsible for handling exception thrown in application
We found that @betsys-nestjs/exception-handling demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.