Esempio n. 1
0
def add_unknown_event_error_to_message(**args) -> tuple:
    message = args.get('message')
    event_type = '[ no event type attribute ]'
    if 'event_type' in message:
        event_type = message['event_type']
    error = dict({"error": "unknown event type: {}".format(event_type)})
    message = add_error_to_message(message, error)
    return True, args
Esempio n. 2
0
def test_add_error_to_message_method_handles_cerberus_errors():
    error_message = json.loads('{"fieldA": ["required", "must be string"]}')
    message_dict = 'some string that is not valid JSON'
    error_dict = {'isSuccess': False, 'errors': error_message}
    modified_message = message.add_error_to_message(message_dict, error_dict['errors'])
    assert isinstance(modified_message, dict)
    assert 'errors' in modified_message
    assert 'timestamp' in modified_message['errors'][0]
    assert 'description' in modified_message['errors'][0]
Esempio n. 3
0
def test_helper_has_add_error_to_message_method():
    message_dict = json.loads('{"event_type": "some invalid event"}')
    expected_error_message = 'error message'
    error_dict = json.loads('{"isSuccess": false, "errors": "error message"}')
    modified_message = message.add_error_to_message(message_dict, error_dict['errors'])
    assert isinstance(modified_message, dict)
    assert 'errors' in modified_message
    assert 'timestamp' in modified_message['errors'][0]
    assert 'description' in modified_message['errors'][0]
    assert modified_message['errors'][0]['description'] == expected_error_message
Esempio n. 4
0
def write(**args):
    config = args.get('config')
    message = args.get('message')
    mapper = Mapper(config)
    tables_for_insert = mapper.convert_to_tables(message)
    is_successful, error = insert(config, tables_for_insert)
    if is_successful:
        return True, args
    logging.warning('database write failed: {}'.format(error))
    args['message'] = msg.add_error_to_message(message, error)
    return False, args
Esempio n. 5
0
 def test_add_error_to_message_method(self):
     message_dict = {'event_type': 'some invalid event'}
     expected_error_message = 'error message'
     error_dict = {'isSuccess': False, 'errors': expected_error_message}
     modified_message = message.add_error_to_message(
         message_dict, error_dict['errors'])
     assert isinstance(modified_message, dict)
     assert 'errors' in modified_message
     assert 'timestamp' in modified_message['errors'][0]
     assert 'description' in modified_message['errors'][0]
     assert modified_message['errors'][0][
         'description'] == expected_error_message
Esempio n. 6
0
    def callback(self, ch, method, properties, body):
        logging.info('message received; callback invoked')

        message_dict = decode_message(body, self.config.ENCRYPT_KEY)

        result = self.validator.validate(message_dict)
        logging.info("write to: " + result['queue'])
        if result['isSuccess']:
            # Validation SUCCESSFUL
            if self.writer.publish(
                    result['queue'],
                    encode_message(message_dict, self.config.ENCRYPT_KEY)):
                ch.basic_ack(delivery_tag=method.delivery_tag)
        else:
            # Validation FAILED
            message_with_errors = add_error_to_message(message_dict, result)
            if self.writer.publish(
                    result['queue'],
                    encode_message(message_with_errors,
                                   self.config.ENCRYPT_KEY)):
                ch.basic_ack(delivery_tag=method.delivery_tag)