added a dedicated error handler service

This commit is contained in:
unknown
2024-10-04 22:04:19 +03:00
parent a311286430
commit 553f08641e
9 changed files with 130 additions and 7 deletions

View File

@ -0,0 +1,40 @@
// src/common/services/error-message.service.ts
import { Injectable } from '@nestjs/common';
type ErrorMessageKey = keyof typeof ErrorMessageService.prototype.messages;
@Injectable()
export class ErrorMessageService {
public readonly messages = {
NOT_FOUND: '{entity} not found', // Single key for "not found" errors
INVALID_MINUTES: 'Invalid minutes value',
INVALID_TIME_FORMAT: 'Invalid time format',
USER_NOT_FOUND: '{entity} not found', // Can reuse NOT_FOUND if desired
INTERNAL_SERVER_ERROR: 'Internal server error',
ERROR_ADDING_TEMP_PASSWORD:
'Error adding {type} temporary password from Tuya',
INVALID_UUID: 'Invalid {entity} UUID',
USER_ALREADY_BELONGS: 'This user already belongs to this {entity}',
USER_HAS_NO_ENTITIES: 'This user has no {entity}',
DEVICE_OPERATION_FAILED: 'All device operations failed',
REQUEST_FAILED: 'Error processing {operation} request',
COOLDOWN_ERROR:
'Please wait {time} more seconds before requesting a new OTP.',
};
getMessage(
key: ErrorMessageKey,
params?: Record<string, string | number>,
): string {
let message = this.messages[key] || 'Unknown error';
// Replace placeholders with provided params
if (params) {
Object.keys(params).forEach((param) => {
const regex = new RegExp(`{${param}}`, 'g');
message = message.replace(regex, params[param].toString());
});
}
return message;
}
}