1+ import { HttpException , HttpStatus } from '@nestjs/common' ;
2+ import { ErrorCode , ErrorMessages } from './error.codes' ;
3+
4+ export class BaseCustomException extends HttpException {
5+ constructor (
6+ public readonly errorCode : ErrorCode ,
7+ message ?: string ,
8+ public readonly details ?: string [ ] ,
9+ statusCode ?: HttpStatus ,
10+ ) {
11+ super (
12+ {
13+ errorCode,
14+ message : message || ErrorMessages [ errorCode ] ,
15+ details,
16+ } ,
17+ statusCode || HttpStatus . BAD_REQUEST ,
18+ ) ;
19+ }
20+ }
21+
22+ // Validation Exceptions
23+ export class ValidationException extends BaseCustomException {
24+ constructor ( details ?: string [ ] , message ?: string ) {
25+ super (
26+ ErrorCode . VALIDATION_ERROR ,
27+ message ,
28+ details ,
29+ HttpStatus . BAD_REQUEST ,
30+ ) ;
31+ }
32+ }
33+
34+ export class InvalidInputException extends BaseCustomException {
35+ constructor ( details ?: string [ ] , message ?: string ) {
36+ super (
37+ ErrorCode . INVALID_INPUT ,
38+ message ,
39+ details ,
40+ HttpStatus . BAD_REQUEST ,
41+ ) ;
42+ }
43+ }
44+
45+ // Authentication Exceptions
46+ export class UnauthorizedException extends BaseCustomException {
47+ constructor ( message ?: string , details ?: string [ ] ) {
48+ super (
49+ ErrorCode . UNAUTHORIZED ,
50+ message ,
51+ details ,
52+ HttpStatus . UNAUTHORIZED ,
53+ ) ;
54+ }
55+ }
56+
57+ export class InvalidCredentialsException extends BaseCustomException {
58+ constructor ( message ?: string ) {
59+ super (
60+ ErrorCode . INVALID_CREDENTIALS ,
61+ message ,
62+ undefined ,
63+ HttpStatus . UNAUTHORIZED ,
64+ ) ;
65+ }
66+ }
67+
68+ export class TokenExpiredException extends BaseCustomException {
69+ constructor ( message ?: string ) {
70+ super (
71+ ErrorCode . TOKEN_EXPIRED ,
72+ message ,
73+ undefined ,
74+ HttpStatus . UNAUTHORIZED ,
75+ ) ;
76+ }
77+ }
78+
79+ // Authorization Exceptions
80+ export class ForbiddenException extends BaseCustomException {
81+ constructor ( message ?: string , details ?: string [ ] ) {
82+ super (
83+ ErrorCode . FORBIDDEN ,
84+ message ,
85+ details ,
86+ HttpStatus . FORBIDDEN ,
87+ ) ;
88+ }
89+ }
90+
91+ export class InsufficientPermissionsException extends BaseCustomException {
92+ constructor ( message ?: string ) {
93+ super (
94+ ErrorCode . INSUFFICIENT_PERMISSIONS ,
95+ message ,
96+ undefined ,
97+ HttpStatus . FORBIDDEN ,
98+ ) ;
99+ }
100+ }
101+
102+ // Resource Exceptions
103+ export class ResourceNotFoundException extends BaseCustomException {
104+ constructor ( resourceType ?: string , message ?: string ) {
105+ const customMessage = message ||
106+ ( resourceType ? `${ resourceType } not found` : undefined ) ;
107+ super (
108+ ErrorCode . RESOURCE_NOT_FOUND ,
109+ customMessage ,
110+ undefined ,
111+ HttpStatus . NOT_FOUND ,
112+ ) ;
113+ }
114+ }
115+
116+ export class UserNotFoundException extends BaseCustomException {
117+ constructor ( userId ?: string ) {
118+ const message = userId
119+ ? `User with ID ${ userId } not found`
120+ : undefined ;
121+ super (
122+ ErrorCode . USER_NOT_FOUND ,
123+ message ,
124+ undefined ,
125+ HttpStatus . NOT_FOUND ,
126+ ) ;
127+ }
128+ }
129+
130+ export class PropertyNotFoundException extends BaseCustomException {
131+ constructor ( propertyId ?: string ) {
132+ const message = propertyId
133+ ? `Property with ID ${ propertyId } not found`
134+ : undefined ;
135+ super (
136+ ErrorCode . PROPERTY_NOT_FOUND ,
137+ message ,
138+ undefined ,
139+ HttpStatus . NOT_FOUND ,
140+ ) ;
141+ }
142+ }
143+
144+ // Conflict Exceptions
145+ export class ConflictException extends BaseCustomException {
146+ constructor ( message ?: string , details ?: string [ ] ) {
147+ super (
148+ ErrorCode . CONFLICT ,
149+ message ,
150+ details ,
151+ HttpStatus . CONFLICT ,
152+ ) ;
153+ }
154+ }
155+
156+ export class DuplicateEntryException extends BaseCustomException {
157+ constructor ( field ?: string , message ?: string ) {
158+ const customMessage = message ||
159+ ( field ? `${ field } already exists` : undefined ) ;
160+ super (
161+ ErrorCode . DUPLICATE_ENTRY ,
162+ customMessage ,
163+ undefined ,
164+ HttpStatus . CONFLICT ,
165+ ) ;
166+ }
167+ }
168+
169+ // Server Exceptions
170+ export class InternalServerException extends BaseCustomException {
171+ constructor ( message ?: string , details ?: string [ ] ) {
172+ super (
173+ ErrorCode . INTERNAL_SERVER_ERROR ,
174+ message ,
175+ details ,
176+ HttpStatus . INTERNAL_SERVER_ERROR ,
177+ ) ;
178+ }
179+ }
180+
181+ export class DatabaseException extends BaseCustomException {
182+ constructor ( message ?: string ) {
183+ super (
184+ ErrorCode . DATABASE_ERROR ,
185+ message ,
186+ undefined ,
187+ HttpStatus . INTERNAL_SERVER_ERROR ,
188+ ) ;
189+ }
190+ }
191+
192+ // Business Logic Exceptions
193+ export class BusinessRuleViolationException extends BaseCustomException {
194+ constructor ( message ?: string , details ?: string [ ] ) {
195+ super (
196+ ErrorCode . BUSINESS_RULE_VIOLATION ,
197+ message ,
198+ details ,
199+ HttpStatus . UNPROCESSABLE_ENTITY ,
200+ ) ;
201+ }
202+ }
203+
204+ export class OperationNotAllowedException extends BaseCustomException {
205+ constructor ( message ?: string ) {
206+ super (
207+ ErrorCode . OPERATION_NOT_ALLOWED ,
208+ message ,
209+ undefined ,
210+ HttpStatus . FORBIDDEN ,
211+ ) ;
212+ }
213+ }
0 commit comments