Commit 0a4d39d7 by gradyward

Exception Handling for the Essay Set module

Other small changes.
parent 6c1aa5f9
......@@ -69,7 +69,7 @@ def create(examples, scores, prompt_string, dump_data=False):
# Create an essay set object that encapsulates all the essays and alternate representations (tokens, etc)
try:
essay_set = _create_essay_set(examples, scores, prompt_string)
except (ExampleCreationRequestError, ExampleCreationInternalError) as ex:
except (EssaySetRequestError, ExampleCreationInternalError) as ex:
msg = "Essay Set Creation failed (likely due to an error in essay cleaning/parsing) {}".format(ex)
results['errors'].append(msg)
log.exception(msg)
......
......@@ -2,7 +2,7 @@
Errors for the EASE repository
"""
class ExampleCreationRequestError(Exception):
class EssaySetRequestError(Exception):
pass
class ExampleCreationInternalError(Exception):
......@@ -18,3 +18,4 @@ class ClassifierTrainingInternalError(Exception):
pass
class CreateRequestError(Exception):
pass
\ No newline at end of file
......@@ -8,6 +8,7 @@ import sys
import random
import os
import logging
from errors import *
base_path = os.path.dirname(__file__)
sys.path.append(base_path)
......@@ -76,6 +77,9 @@ class EssaySet(object):
Returns:
A string confirmation that essay was added.
Raises
EssaySetRequestError
"""
# Get maximum current essay id (the newest essay), or set to 0 if this is the first essay added
......@@ -91,9 +95,10 @@ class EssaySet(object):
except UnicodeError:
try:
essay_text = (essay_text.decode('utf-8', 'replace')).encode('ascii', 'ignore')
except UnicodeError:
log.exception("Could not parse essay into ascii.")
raise
except UnicodeError as ex:
str = "Could not parse essay text into ascii: {}".format(ex)
log.exception(str)
raise EssaySetRequestError(ex)
# Validates that score is an integer and essay_text is a string.
try:
......@@ -101,15 +106,15 @@ class EssaySet(object):
essay_text = str(essay_text)
essay_generated = int(essay_generated)
except TypeError:
log.exception(
"Invalid type for essay score : {0} or essay text : {1}".format(type(essay_score), type(essay_text)))
raise
str = "Invalid type for essay score : {0} or essay text : {1}".format(type(essay_score), type(essay_text))
log.exception(str)
raise EssaySetRequestError(str)
# Validates that essay generated is 0 or 1
if essay_generated != 0 and essay_generated != 1:
ex = "Invalid value for essay_generated ({}). Value must be 0 or 1.".format(essay_generated)
log.exception(ex)
raise util_functions.InputError(ex)
raise EssaySetRequestError(ex)
# Validates to make sure that the essay is at least five characters long.
if len(essay_text) < 5:
......@@ -159,8 +164,11 @@ class EssaySet(object):
Returns:
(str): The prompt, if it was stored successfully.
Raises:
InputError
"""
if (isinstance(prompt_text, basestring)):
if isinstance(prompt_text, basestring):
self._prompt = util_functions.sub_chars(prompt_text)
else:
raise util_functions.InputError(prompt_text, "Invalid prompt. Need to enter a string value.")
......
......@@ -89,9 +89,9 @@ def spell_correct(string):
incorrect = p.readlines()
p.close()
except Exception:
log.exception("aspell process failed; could not spell check")
# Return original string if aspell fails
except Exception as ex:
log.exception("aspell spell checking was not run, because it failed with the exception: {}".format(ex))
# DESIGN CHOICE: Return original string if aspell fails
return string, 0, string
finally:
......@@ -201,6 +201,9 @@ def get_vocab(essays, scores, max_features_pass_1=750, max_features_pass_2=200):
class InputError(Exception):
"""
A class to report to the user that one of their inputs was incorrect.
"""
def __init__(self, expr, msg):
self.expr = expr
self.msg = msg
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment