<commit_before>import json
import operation_builder
from pymongo import MongoClient
from bson.objectid import ObjectId

class transaction:

	def __init__(self):
		self.transactionID = -1
		self.library = None
		self.operation = None
		self.data = None
		self.results = None

	def parse_json(self, json_data):
		try:
			input_data = json.loads(json_data)
			self.transactionID = input_data['transactionID']
			self.operation = input_data['operation']
			self.library = input_data['library']
			
			dataID = input_data['data']
			corpora = MongoClient().linguine.corpora
			self.data = corpora.find_one({"_id" : ObjectId(dataID)})['text']

			return True
		except TypeError:
			return False

	def run(self):
		if self.operation == None:
			print("no")
			return False
		try:
			op_handler = operation_builder.get_operation_handler(self.operation)
			self.results = op_handler.run(self.data)
			return self.results
		except RuntimeError:
			print("couldn't build")
			return False
		
	def get_json_response(self):
		resultsCollection = MongoClient().linguine.results
		resultID = resultsCollection.insert(self.results)
		response = {'transactionID':self.transactionID, 'library':self.library, 'operation':self.operation, 'results':str(resultID)}<commit_msg>Improved transaction error handling<commit_after>import json
import operation_builder
from pymongo import MongoClient
from bson.objectid import ObjectId

class transaction:

	def __init__(self):
		self.transactionID = -1
		self.library = None
		self.operation = None
		self.data = None
		self.results = None
		self.error = None

	def parse_json(self, json_data):
		try:
			input_data = json.loads(json_data)
			self.transactionID = input_data['transactionID']
			self.operation = input_data['operation']
			self.library = input_data['library']
			dataID = input_data['data']
		except TypeError:
			self.error = "Improperly formatted request"
			return False

		try:
			corpora = MongoClient().linguine.corpora
			self.data = corpora.find_one({"_id" : ObjectId(dataID)})['text']
			return True
		except TypeError:
			self.error = "Could not find requested data ID"
			return False

	def run(self):
		if self.operation == None:
			self.error = "No operation indicated"
			return False
		try:
			op_handler = operation_builder.get_operation_handler(self.operation)
			self.results = op_handler.run(self.data)
			return self.results
		except RuntimeError:
			self.error = "Invalid operation requested"
			return False
		
	def get_json_response(self):
		resultsCollection = MongoClient().linguine.results
		resultID = resultsCollection.insert(self.results)
		response = {'transactionID':self.transactionID, 'library':self.library, 'operation':self.operation, 'results':str(resultID)}
		if not self.error == None:
			response['error'] = self.error
{'fraction_non_alphanumeric': 0.10697674418604651, 'fraction_numerical': 0.0007155635062611807, 'mean_word_length': 4.558648111332008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1025265', 'n_tokens_mistral': 980, 'n_tokens_neox': 846, 'n_words': 197}