feat(core): polish codebase, enhance error handling, security hardening, rate limiting, and a11y compliance
Deploy Canina / deploy (push) Successful in 1m43s
Deploy Canina / deploy (push) Successful in 1m43s
This commit is contained in:
1 parent
70c2627d93
commit
7b89e141c5
17 files changed
+237
-17
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import {
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
validateSync,
|
||||
} from 'class-validator';
|
||||
|
||||
class EnvironmentVariables {
|
||||
@IsNotEmpty({ message: 'DATABASE_URL is required for database connection' })
|
||||
@IsString()
|
||||
DATABASE_URL: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
NODE_ENV?: string = 'development';
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
PORT?: string = '4001';
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
JWT_ACCESS_SECRET?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
JWT_REFRESH_SECRET?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
REDIS_HOST?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
REDIS_PORT?: string;
|
||||
}
|
||||
|
||||
export function validateEnv(config: Record<string, unknown>) {
|
||||
const validatedConfig = plainToInstance(EnvironmentVariables, config, {
|
||||
enableImplicitConversion: true,
|
||||
});
|
||||
|
||||
const errors = validateSync(validatedConfig, {
|
||||
skipMissingProperties: false,
|
||||
});
|
||||
|
||||
if (errors.length > 0) {
|
||||
const messages = errors
|
||||
.map((err) => Object.values(err.constraints || {}).join(', '))
|
||||
.join('; ');
|
||||
throw new Error(`Environment Validation Error: ${messages}`);
|
||||
}
|
||||
|
||||
return validatedConfig;
|
||||
}
|
||||
@@ -72,11 +72,10 @@ export class CustomHttpExceptionFilter implements ExceptionFilter {
|
||||
}
|
||||
|
||||
const errorPayload = {
|
||||
success: false,
|
||||
statusCode: status,
|
||||
message,
|
||||
code,
|
||||
details,
|
||||
error: code,
|
||||
details: details && Object.keys(details).length > 0 ? details : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
path: request.url,
|
||||
};
|
||||
|
||||
@@ -54,16 +54,24 @@ export class PrismaExceptionFilter implements ExceptionFilter {
|
||||
}
|
||||
}
|
||||
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
if (isProd && status === HttpStatus.INTERNAL_SERVER_ERROR) {
|
||||
message =
|
||||
'خطایی در پردازش اطلاعات پایگاه داده رخ داد. لطفاً بعداً تلاش فرمایید.';
|
||||
details = {};
|
||||
} else if (isProd && details && (details as any).meta) {
|
||||
delete (details as any).meta;
|
||||
}
|
||||
|
||||
this.logger.error(
|
||||
`[Prisma ${exception.code}] ${request.method} ${request.url} - ${message}`,
|
||||
`[Prisma ${exception.code}] ${request.method} ${request.url} - ${exception.message}`,
|
||||
);
|
||||
|
||||
response.status(status).json({
|
||||
success: false,
|
||||
statusCode: status,
|
||||
message,
|
||||
code,
|
||||
details,
|
||||
error: code,
|
||||
details: Object.keys(details).length > 0 ? details : undefined,
|
||||
timestamp: new Date().toISOString(),
|
||||
path: request.url,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user