Skip to content

Error Keys Standard in NestKit

NestKit enforces a specific error keys standard to improve logging and error tracing capabilities. Adhering to this standard helps maintain consistency and readability across your application.

All error keys are registered in a common enum, and each key follows the standard format:

{PRODUCT}_{RESOURCE_PLURAL}_{ERROR}

Error Keys Enum Example

ts
// enums/error-keys.ts

export enum ErrorKeys {
    LIB_BOOKS_NOT_FOUND = 'LIB_BOOKS_NOT_FOUND',
    LIB_BOOKS_CREATE_FAILED = 'LIB_BOOKS_CREATE_FAILED',
    LIB_BOOKS_UPDATE_FAILED = 'LIB_BOOKS_UPDATE_FAILED',
    LIB_BOOKS_DELETE_FAILED = 'LIB_BOOKS_DELETE_FAILED',
}

How to Use Error Keys

When handling errors, catch the error and specify the error key in the exception payload.

ts
    try {
      await this.bookRepository.save(book);
    } catch (error) {
      throw new InternalServerErrorException({
        message: 'Failed to create book',
        key: ErrorKeys.LIB_BOOKS_CREATE_FAILED,
        context: logger.getContext(),
        error,
      });
    }

By using the NestKit error keys standard, you can create more structured and traceable error handling throughout your application. This approach makes it easier to identify and fix issues, as well as improve the overall development experience.

Created by DeepVision Software.