"""Lambda function module.""" import config from src.constants import general from src.common import lambda_exceptions from src.common import s3 from src.common import status from src.common import util from src.common import validators from src.common.constants import errors from src.common.constants import transcoding_types from src.common.lambda_exceptions import LambdaStatusError class EncodingRouteError(LambdaStatusError): """Error raised for an invalid transcoding type.""" def _log_error(err, filename, bucket): status.send_general_status( function=config.LAMBDA_NAME, status_name=general.ENCODING_ROUTE_ERROR_STATUS, filename=filename, bucket=bucket, errors=err ) def handler(event, context): """Lambda entry point.""" try: bucket, key, asset_config = util.extract_encoding_route_event(event) input_object = s3.head_object(bucket=bucket, key=key) transcoding_type, file_type = validators.get_transcoding_type(input_object) if transcoding_type == transcoding_types.UNKNOWN_TRANSCODING_TYPE \ or file_type == transcoding_types.UNKNOWN_FILE_TYPE: raise EncodingRouteError( error_code=errors.ASSET_TYPE_ROUTING_ERROR_CODE, error_params={ 'transcoding_type': transcoding_type, 'file_type': file_type } ) status.send_general_status( function=config.LAMBDA_NAME, status_name=general.ENCODING_ROUTE_COMPLETE_STATUS, filename=key, bucket=bucket ) return { 'bucket': bucket, 'key': key, 'transcoding_type': transcoding_type, 'file_type': file_type, 'config': asset_config } except lambda_exceptions.LambdaStatusError as error: config.logger.exception(str(error)) # if this is a malformed event but we have the key, we can send the error to the microserivce. # if the key is missing, then we cant post the status if 'key' in event: _log_error(error.errors, event.get('key'), event.get('bucket', None)) raise except lambda_exceptions.LambdaError as error: config.logger.exception(str(error)) raise except Exception as error: config.logger.exception(str(error)) e = dict() e[errors.ASSET_ROUTING_ERROR_CODE] = {'message': str(error)} _log_error(e, key, bucket) raise