"""Exceptions module for lambdas.""" from constants import errors import status class UnexpectedEventBody(Exception): """ Exceptions that raises if input event doesn't have needed arguments. Attributes: expected_args (list): Expected event body arguments. """ def __init__(self, expected_args): """Show exception message. Args: expected_args (list): Expected event body arguments. """ args_sting = ' '.join(expected_args) super().__init__( self, ("Expected event arguments: {args} haven't been provided" .format(args=args_sting)) ) class ModelValidationFailed(Exception): """ Exceptions raises if SQLAlchemy model object didn't pass validation rules. Attributes: field_name (str): Field that didn't pass through the validation. error_description (str): What exactly is wrong with a data in the field. """ def __init__(self, field_name, error_description): """Show exception message. Args: field_name (list): Expected event body arguments. """ super().__init__( self, ('Field {0} contains invalid data: {1}.'.format( field_name, error_description))) class UnexpectedFileHeaders(Exception): """Exception raises if file headers don't match allowed set of headers.""" error_code = errors.INVALID_FILE_STRUCTURE_ERROR_CODE def __init__(self): """Show exception message.""" super().__init__( self, ( 'Headers in provided sales data file don\'t match ' 'neither Phonofile nor Finetunes list of headers.')) class ContentDoesNotMatchHeaders(Exception): """Exception raises if file content doesn't match file headers.""" error_code = errors.CONTENT_DOES_NOT_MATCH_HEADERS_ERROR_CODE def __init__(self): """Show exception message.""" super().__init__( self, 'Data in some columns doesn\'t match headers.') class ContentIsMissing(Exception): """Exception raises if there is no content, but headers in file.""" error_code = errors.MISSING_DATA_ERROR_CODE def __init__(self): """Show exception message.""" super().__init__( self, 'There is no content in the provided sales file.') class MandatoryFieldsMissing(Exception): """Exception raises if required fields in row are missing.""" error_code = errors.MISSING_MANDATORY_FIELDS_ERROR_CODE def __init__(self, missing_fields): """Show exception message. Args: missing_fields (list): list of strings (missing fields). """ self.missing_fields = missing_fields super().__init__( self, 'Required fields are missing: {0}.'.format( ','.join(missing_fields))) class FileEncodingNotIdentified(Exception): """Exception raises if file has unexpected encoding.""" error_code = errors.ENCODING_NOT_IDENTIFIED_ERROR_CODE def __init__(self): """Show exception message.""" super().__init__( self, ( 'Unexpected encoding is used in the sales file.')) class LoggedException(Exception): """Exception to avoid propagation of general notification messages.""" class DataValidationFailed(Exception): """Exception raises if data validation failed.""" error_code = errors.DATA_VALIDATION_ERROR_CODE invalid_fields = None def __init__(self, invalid_fields): """Show exception message.""" self.invalid_fields = invalid_fields err_string = [] for key, value in invalid_fields.items(): res = 'Row {}: {}. '.format(key, ', '.join(value)) err_string.append(res) err_desc_string = ', '.join(err_string) super().__init__( self, ( 'Following fields contain invalid data: {}.'.format( err_desc_string))) def notify_and_raise( function, error_status, error_code, filename, bucket, error_params=None): """Send status notification and raise exception. Args: function (str): Lambda function name that causes an error. error_status (str): Error status for lambda function. error_code (str): Error code. filename (str): Current processed publishing filename. bucket (str): Current processed publishing source bucket name. error_params (dict): Additional error params. """ if not error_params: error_params = {} if isinstance(error_params.get('message'), list): error_params['message'] = '; '.join(error_params['message']) error_message = errors.ERROR_MESSAGES.get( error_code, 'Unknown Error').format(**error_params) status.send_failure_status( function, error_status, filename, error_code, error_message, bucket) raise LoggedException(error_message)