When using transactions with TypeORM in NestJS, you can capture transaction errors and handle them appropriately. Generally, there are several methods to capture and handle these errors:
Using try/catch Blocks
In TypeORM, you may use queryRunner to create and manage transactions. In this case, you can use try/catch blocks to capture any errors that occur during the transaction.
For example:
typescriptimport { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, Connection } from 'typeorm'; import { YourEntity } from './entities/your.entity'; @Injectable() export class YourService { constructor( @InjectRepository(YourEntity) private yourEntityRepository: Repository<YourEntity>, private connection: Connection ) {} async someTransactionalMethod(): Promise<void> { const queryRunner = this.connection.createQueryRunner(); await queryRunner.connect(); await queryRunner.startTransaction(); try { // This is your transaction logic // ... await queryRunner.commitTransaction(); } catch (err) { // If an error occurs during the transaction, it will be captured here await queryRunner.rollbackTransaction(); // You can handle the error here throw new Error(`Transaction failed: ${err.message}`); } finally { // You need to release the queryRunner await queryRunner.release(); } } }
Using Transaction Decorators
When integrating NestJS with TypeORM, decorators such as @Transaction() and @TransactionManager() are provided, which can be used on methods to automatically handle transactions. If errors occur within these transactions, TypeORM will automatically roll back the transaction, and errors can be captured through standard error handling mechanisms (such as global exception filters or try/catch within the method).
For example:
typescriptimport { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, EntityManager, Transaction, TransactionManager } from 'typeorm'; import { YourEntity } from './entities/your.entity'; @Injectable() export class YourService { constructor( @InjectRepository(YourEntity) private yourEntityRepository: Repository<YourEntity> ) {} @Transaction() async someTransactionalMethod( @TransactionManager() manager: EntityManager ): Promise<void> { try { // Use the manager to perform operations, for example: await manager.save(YourEntity, /* ... */); // ... } catch (err) { // If an error occurs, TypeORM will automatically roll back the transaction // You can handle the error here throw new Error(`Transaction failed: ${err.message}`); } } }
In the above two methods, if transaction errors occur, you can handle them by throwing custom errors or using NestJS's built-in exception filters (such as HttpException or more specific exception classes). Additionally, you can further customize error handling logic within the exception filters, for example, logging or sending alerts. Remember, error handling is an important aspect and should be designed appropriately based on specific application scenarios.