'Converts failed result information to output format.
Traceback information is truncated or not included if it would result in an output string
that would not fit in the database.  If the output is still too long, then the
exception message is also truncated.
Truncation is indicated by adding "..." to the end of the value.'

@staticmethod
def create_output_for_failure(exception, traceback_string):

    tag = '...'
    task_progress = {'exception': type(exception).__name__, 'message': unicode(exception.message)}
    if (traceback_string is not None):
        task_progress['traceback'] = traceback_string
    json_output = json.dumps(task_progress)
    too_long = (len(json_output) - 1023)
    if (too_long > 0):
        if (traceback_string is not None):
            if (too_long >= (len(traceback_string) - len(tag))):
                del task_progress['traceback']
                too_long -= (len(traceback_string) + len('traceback'))
            else:
                task_progress['traceback'] = (traceback_string[:(- (too_long + len(tag)))] + tag)
                too_long = 0
        if (too_long > 0):
            task_progress['message'] = (task_progress['message'][:(- (too_long + len(tag)))] + tag)
        json_output = json.dumps(task_progress)
    return json_output
