'Converts the headless\' error from JSON into a more informative
python HeadlessJSJavascriptError.'

def browser_error_to_exception(self, script_path, stdout_output):

    try:
        js_test_results = json.loads(stdout_output)
        last_error = js_test_results['errors'][0]
        err_string = ('%s\n%s' % (last_error['msg'], self.browser_backtrace_to_string(last_error['backtrace'])))
    except ValueError as val_err:
        if (str(val_err) == 'No    JSON    object    could    be    decoded'):
            log.debug('(error    parsing    returned    JSON    from    casperjs,    dumping    stdout...)\n:%s', stdout_output)
            return HeadlessJSJavascriptError('see    log    for    details')
        else:
            raise
    except Exception as exc:
        log.debug('(failed    to    parse    error    returned    from    %s:    %s)', _PATH_TO_HEADLESS, str(exc))
        return HeadlessJSJavascriptError(('ERROR    in    headless    browser    script    %s' % script_path))
    return HeadlessJSJavascriptError(err_string)
