Merge branch 'handleAdErrors' into 'main'

publish errors for invalid ads

See merge request v3/service/matcher!6
This commit is contained in:
Sylvain Briat 2023-05-24 14:28:47 +00:00
commit 0297055ece
8 changed files with 84 additions and 44 deletions

View File

@ -6,6 +6,8 @@ import { CreateAdRequest } from '../../domain/dtos/create-ad.request';
import { validateOrReject } from 'class-validator';
import { Messager } from '../secondaries/messager';
import { plainToInstance } from 'class-transformer';
import { DatabaseException } from 'src/modules/database/exceptions/database.exception';
import { ExceptionCode } from 'src/modules/utils/exception-code.enum';
@Controller()
export class AdMessagerController {
@ -18,12 +20,10 @@ export class AdMessagerController {
name: 'adCreated',
})
async adCreatedHandler(message: string): Promise<void> {
let createAdRequest: CreateAdRequest;
// parse message to request instance
try {
// parse message to request instance
const createAdRequest: CreateAdRequest = plainToInstance(
CreateAdRequest,
JSON.parse(message),
);
createAdRequest = plainToInstance(CreateAdRequest, JSON.parse(message));
// validate instance
await validateOrReject(createAdRequest);
// validate nested objects (fixes direct nested validation bug)
@ -34,8 +34,40 @@ export class AdMessagerController {
throw e;
}
}
} catch (e) {
this.messager.publish(
'matcher.ad.crit',
JSON.stringify({
message: `Can't validate message : ${message}`,
error: e,
}),
);
}
try {
await this.commandBus.execute(new CreateAdCommand(createAdRequest));
} catch (e) {
if (e instanceof DatabaseException) {
if (e.message.includes('already exists')) {
this.messager.publish(
'matcher.ad.crit',
JSON.stringify({
code: ExceptionCode.ALREADY_EXISTS,
message: 'Already exists',
uuid: createAdRequest.uuid,
}),
);
}
if (e.message.includes("Can't reach database server")) {
this.messager.publish(
'matcher.ad.crit',
JSON.stringify({
code: ExceptionCode.UNAVAILABLE,
message: 'Database server unavailable',
uuid: createAdRequest.uuid,
}),
);
}
}
this.messager.publish(
'logging.matcher.ad.crit',
JSON.stringify({

View File

@ -11,8 +11,8 @@ import { Configuration } from '../../domain/entities/configuration';
@Controller()
export class ConfigurationMessagerController {
constructor(
private readonly _commandBus: CommandBus,
private readonly _configService: ConfigService,
private readonly commandBus: CommandBus,
private readonly configService: ConfigService,
) {}
@RabbitSubscribe({
@ -22,14 +22,14 @@ export class ConfigurationMessagerController {
const configuration: Configuration = JSON.parse(message);
if (
configuration.domain ==
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const setConfigurationRequest: SetConfigurationRequest =
new SetConfigurationRequest();
setConfigurationRequest.domain = configuration.domain;
setConfigurationRequest.key = configuration.key;
setConfigurationRequest.value = configuration.value;
await this._commandBus.execute(
await this.commandBus.execute(
new SetConfigurationCommand(setConfigurationRequest),
);
}
@ -42,12 +42,12 @@ export class ConfigurationMessagerController {
const deletedConfiguration: Configuration = JSON.parse(message);
if (
deletedConfiguration.domain ==
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const deleteConfigurationRequest = new DeleteConfigurationRequest();
deleteConfigurationRequest.domain = deletedConfiguration.domain;
deleteConfigurationRequest.key = deletedConfiguration.key;
await this._commandBus.execute(
await this.commandBus.execute(
new DeleteConfigurationCommand(deleteConfigurationRequest),
);
}
@ -61,14 +61,14 @@ export class ConfigurationMessagerController {
configurations.forEach(async (configuration) => {
if (
configuration.domain ==
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
this.configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const setConfigurationRequest: SetConfigurationRequest =
new SetConfigurationRequest();
setConfigurationRequest.domain = configuration.domain;
setConfigurationRequest.key = configuration.key;
setConfigurationRequest.value = configuration.value;
await this._commandBus.execute(
await this.commandBus.execute(
new SetConfigurationCommand(setConfigurationRequest),
);
}

View File

@ -5,19 +5,19 @@ import { IConfigurationRepository } from '../../domain/interfaces/configuration.
@Injectable()
export class RedisConfigurationRepository extends IConfigurationRepository {
constructor(@InjectRedis() private readonly _redis: Redis) {
constructor(@InjectRedis() private readonly redis: Redis) {
super();
}
async get(key: string): Promise<string> {
return await this._redis.get(key);
}
get = async (key: string): Promise<string> => {
return await this.redis.get(key);
};
async set(key: string, value: string) {
await this._redis.set(key, value);
}
set = async (key: string, value: string): Promise<void> => {
await this.redis.set(key, value);
};
async del(key: string) {
await this._redis.del(key);
}
del = async (key: string): Promise<void> => {
await this.redis.del(key);
};
}

View File

@ -41,14 +41,17 @@ import { SetConfigurationUseCase } from './domain/usecases/set-configuration.use
setConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: ['configuration.create', 'configuration.update'],
queue: 'matcher-configuration-create-update',
},
deleteConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.delete',
queue: 'matcher-configuration-delete',
},
propagateConfiguration: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'configuration.propagate',
queue: 'matcher-configuration-propagate',
},
},
uri: configService.get<string>('RMQ_URI'),

View File

@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export abstract class IConfigurationRepository {
abstract get(key: string): Promise<string>;
abstract set(key: string, value: string): void;
abstract del(key: string): void;
abstract set(key: string, value: string): Promise<void>;
abstract del(key: string): Promise<void>;
}

View File

@ -4,13 +4,15 @@ import { DeleteConfigurationCommand } from '../../commands/delete-configuration.
@CommandHandler(DeleteConfigurationCommand)
export class DeleteConfigurationUseCase {
constructor(private _configurationRepository: RedisConfigurationRepository) {}
constructor(private configurationRepository: RedisConfigurationRepository) {}
async execute(deleteConfigurationCommand: DeleteConfigurationCommand) {
await this._configurationRepository.del(
deleteConfigurationCommand.deleteConfigurationRequest.domain +
':' +
deleteConfigurationCommand.deleteConfigurationRequest.key,
execute = async (
deleteConfigurationCommand: DeleteConfigurationCommand,
): Promise<void> => {
await this.configurationRepository.del(
`${deleteConfigurationCommand.deleteConfigurationRequest.domain}
:
${deleteConfigurationCommand.deleteConfigurationRequest.key}`,
);
}
};
}

View File

@ -4,11 +4,12 @@ import { GetConfigurationQuery } from '../../queries/get-configuration.query';
@QueryHandler(GetConfigurationQuery)
export class GetConfigurationUseCase {
constructor(private _configurationRepository: RedisConfigurationRepository) {}
constructor(private configurationRepository: RedisConfigurationRepository) {}
async execute(getConfigurationQuery: GetConfigurationQuery): Promise<string> {
return this._configurationRepository.get(
getConfigurationQuery.domain + ':' + getConfigurationQuery.key,
execute = async (
getConfigurationQuery: GetConfigurationQuery,
): Promise<string> =>
this.configurationRepository.get(
`${getConfigurationQuery.domain}:${getConfigurationQuery.key}`,
);
}
}

View File

@ -4,14 +4,16 @@ import { SetConfigurationCommand } from '../../commands/set-configuration.comman
@CommandHandler(SetConfigurationCommand)
export class SetConfigurationUseCase {
constructor(private _configurationRepository: RedisConfigurationRepository) {}
constructor(private configurationRepository: RedisConfigurationRepository) {}
async execute(setConfigurationCommand: SetConfigurationCommand) {
await this._configurationRepository.set(
setConfigurationCommand.setConfigurationRequest.domain +
':' +
setConfigurationCommand.setConfigurationRequest.key,
execute = async (
setConfigurationCommand: SetConfigurationCommand,
): Promise<void> => {
await this.configurationRepository.set(
`${setConfigurationCommand.setConfigurationRequest.domain}
:
${setConfigurationCommand.setConfigurationRequest.key}`,
setConfigurationCommand.setConfigurationRequest.value,
);
}
};
}