"""Error handling utils.""" from lambdacommon.graphql.graphql import GraphQLError infra_errors = [ 'HTTP Error 504: Gateway Time-out' ] def graphql_execute( graphql_gateway: object, query: str, payload: dict, logger: object) -> dict: """Run a GraphQL query and handle known errors.""" try: logger.info(f'Running GraphQL query: {query} with payload: {payload}') result = graphql_gateway.execute(query, payload) logger.info(f'GraphQL query returned: {result}') return result except GraphQLError as err: logger.info(f'GraphQL query failed with error: {err}') retry_on_infra_failure(err, logger) raise err def retry_on_infra_failure(err: GraphQLError, logger: object): """Retries if the GraphQL error is caused by an infrastructure issue.""" error_message = str(err) for infra_error in infra_errors: if infra_error in error_message: logger.info(f'Retry Lambda for known error: {infra_error}') raise RetryLambdaException(error_message) class RetryLambdaException(Exception): """Exception that terraform handles by running the Lambda again."""