乐闻世界logo
搜索文章和话题

How can I handle TypeORM error in NestJS?

1个答案

1

When handling TypeORM errors in NestJS, following best practices can help you effectively identify and resolve issues. Below are key steps to manage these errors:

1. Error Capture

First, ensure your code includes appropriate error handling logic during database operations. Using try-catch blocks captures exceptions that occur while interacting with the database.

typescript
try { // Database operation, for example, saving user information await userRepository.save(user); } catch (error) { // Error handling logic }

2. Error Identification

Within the catch block, identify the error type based on the error object. TypeORM errors typically provide detailed information, including error codes and messages.

typescript
catch (error) { if (error instanceof QueryFailedError) { // Handle query failure error } else if (error.name === 'EntityNotFoundError') { // Handle entity not found error } else { // Handle other error types } }

3. Logging

Logging error information is critical for developers to trace the root cause. Use NestJS's built-in Logger or integrate a third-party logging service.

typescript
import { Logger } from '@nestjs/common'; const logger = new Logger('TypeORM'); catch (error) { logger.error(`Database operation failed: ${error.message}`, error.stack); }

4. Refining Feedback

Directly returning error details to clients may be unsafe or unuser-friendly. Instead, create custom messages to enhance user experience.

typescript
catch (error) { let message = 'Database operation failed. Please try again later.'; if (error.code === '23505') { message = 'This record already exists. Please avoid duplicates.'; } // Send response to client return { success: false, message }; }

5. Transaction Management

For complex scenarios involving multiple operations, transactions ensure data consistency. If an error occurs, roll back all operations to maintain data integrity.

typescript
const queryRunner = connection.createQueryRunner(); await queryRunner.connect(); await queryRunner.startTransaction(); try { // Execute database operations await queryRunner.manager.save(user); await queryRunner.manager.save(profile); // Commit transaction await queryRunner.commitTransaction(); } catch (error) { // Roll back transaction await queryRunner.rollbackTransaction(); } finally { // Release query runner await queryRunner.release(); }

6. Using Interceptors or Filters

In NestJS, implement interceptors (Interceptor) or exception filters (Exception Filter) for global error handling. This reduces code duplication and ensures consistent error handling across the application.

typescript
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch(QueryFailedError) export class TypeOrmExceptionFilter implements ExceptionFilter { catch(exception: QueryFailedError, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); // Custom response body response.status(500).json({ statusCode: 500, message: 'Database operation encountered an exception.', }); } }

By following these steps, you can effectively manage TypeORM errors in your NestJS application, providing appropriate feedback during database issues while maintaining a positive user experience.

2024年6月29日 12:07 回复

你的答案