{"id":18867939,"url":"https://github.com/hodfords-solutions/nestjs-exception","last_synced_at":"2025-08-09T15:13:21.080Z","repository":{"id":257802467,"uuid":"560811080","full_name":"hodfords-solutions/nestjs-exception","owner":"hodfords-solutions","description":"A NestJS module for handling and customizing exceptions with ease","archived":false,"fork":false,"pushed_at":"2025-03-19T11:08:36.000Z","size":348,"stargazers_count":41,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-06T07:22:01.928Z","etag":null,"topics":["exception","nestjs","nodejs"],"latest_commit_sha":null,"homepage":"https://opensource.hodfords.uk/nestjs-exception","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hodfords-solutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-11-02T10:18:26.000Z","updated_at":"2025-07-04T13:31:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"18d1aa3f-388a-401c-99df-5063f1cd74d1","html_url":"https://github.com/hodfords-solutions/nestjs-exception","commit_stats":null,"previous_names":["hodfords-solutions/nestjs-exception"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/hodfords-solutions/nestjs-exception","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-exception","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-exception/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-exception/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-exception/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hodfords-solutions","download_url":"https://codeload.github.com/hodfords-solutions/nestjs-exception/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hodfords-solutions%2Fnestjs-exception/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269593922,"owners_count":24443829,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-09T02:00:10.424Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exception","nestjs","nodejs"],"created_at":"2024-11-08T05:11:55.213Z","updated_at":"2025-08-09T15:13:21.036Z","avatar_url":"https://github.com/hodfords-solutions.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"http://opensource.hodfords.uk\" target=\"blank\"\u003e\u003cimg src=\"https://opensource.hodfords.uk/img/logo.svg\" width=\"320\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\nThis repository contains a set of custom exception filters and exceptions designed for use in a NestJS application. It enhances error handling for HTTP-based and microservice-based (gRPC, Kafka) applications by providing more meaningful error responses and localization support.\n\n\u003c/p\u003e\n\n## Installation 🤖\n\nTo begin using it, we first install the required dependencies.\n\n```\nnpm install @hodfords/nestjs-exception\n```\n\n## Exception Classes\n\n\u003e **Note**: These exception classes only function when used alongside the `HttpExceptionFilter` or one of its child classes (`GrpcExceptionFilter`, `KafkaExceptionFilter`, etc.). Be sure to apply the appropriate filter in your application.\n\n**1\\. UuidException**\n\nThis exception is used to handle invalid UUID formats in requests. It returns a 400 `BAD_REQUEST` status.\n\nParameters:\n\n- `field`: The name of the field that contains the invalid UUID. This value is passed to indicate which field caused the exception.\n\n**2\\. ValidateException**\n\nHandles specific validation errors related to a particular field. Returns a 422 `UNPROCESSABLE_ENTITY` status.\n\nParameters:\n\n- `property`: The field name that caused the validation error.\n- `message`: The detailed message for the validation error.\n- `constraint`: The validation constraint that was violated (e.g., notNull).\n- `detail`: Additional information for the validation error, if applicable.\n\n## Exception Filters\n\n- **HttpExceptionFilter**: Handles various types of HTTP exceptions with localization support.\n\n- **GrpcExceptionFilter**: Handles exceptions for gRPC microservices, formatting errors for gRPC clients.\n\n- **KafkaExceptionFilter**: Manages exceptions in Kafka microservices, formatting errors for Kafka messaging.\n\n- **ValidatorExceptionFilter**: Catches validation errors (`ValidateException`), supporting nested object validation and localization.\n\n**Note on Translation**: These filters, especially `HttpExceptionFilter` and `ValidatorExceptionFilter`, rely on a translation service to provide localized error messages. Ensure that your application has translation support enabled (e.g., using `@hodfords/nestjs-cls-translation`). The filters use translation keys defined in your language files to dynamically translate error messages based on the request's language.\n\n## Example of usage\n\nTo use the exception classes and filters in your NestJS application, follow these steps:\n\n#### 1\\. **Applying the `HttpExceptionFilter`**\n\n**Global Application:**\n\n```typescript\nimport { HttpExceptionFilter } from '@hodfords/nestjs-exception';\nimport { NestFactory } from '@nestjs/core';\nimport { AppModule } from './app.module';\n\nasync function bootstrap() {\n    const app = await NestFactory.create(AppModule);\n    app.useGlobalFilters(new HttpExceptionFilter());\n    await app.listen(3000);\n}\nbootstrap();\n```\n\n**Controller-Level Application:**\n\n```typescript\nimport { Controller, UseFilters } from '@nestjs/common';\nimport { HttpExceptionFilter } from 'hodfords/nestjs-exception';\n\n@Controller('users')\n@UseFilters(HttpExceptionFilter)\nexport class UserController {}\n```\n\n#### 2\\. **Throwing a Custom Exception**\n\n```typescript\nimport { UuidException } from '@hodfords/nestjs-exception';\n\n@Controller('users')\nexport class UserController {\n    @Get(':id')\n    getUser(@Param('id') id: string) {\n        if (!isValidUUID(id)) {\n            throw new UuidException('id'); // Translation key: 'error.field_malformed'\n        }\n        return { id };\n    }\n}\n```\n\n## License\n\nThis project is licensed under the MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodfords-solutions%2Fnestjs-exception","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhodfords-solutions%2Fnestjs-exception","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhodfords-solutions%2Fnestjs-exception/lists"}