Python json.decoder.JSONDecodeError() Examples

The following are 30 code examples of json.decoder.JSONDecodeError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module json.decoder , or try the search function .
Example #1
Source File: bragi_client.py    From idunn with Apache License 2.0 6 votes vote down vote up
def raw_autocomplete(self, params, body=None):
        url = settings["BRAGI_BASE_URL"] + "/autocomplete"
        if body:
            response = await self.client.post(url, params=params, json=body)
        else:
            response = await self.client.get(url, params=params)

        if response.status_code != httpx.codes.ok:
            try:
                explain = response.json()["long"]
            except (IndexError, JSONDecodeError):
                explain = response.text
            logger.error(
                'Request to Bragi returned with unexpected status %d: "%s"',
                response.status_code,
                explain,
            )
            raise HTTPException(503, "Unexpected geocoder error")

        try:
            return response.json()
        except (JSONDecodeError, pydantic.ValidationError) as e:
            logger.exception("Autocomplete invalid response")
            raise HTTPException(503, "Invalid response from the geocoder") 
Example #2
Source File: automation.py    From eNMS with GNU General Public License v3.0 6 votes vote down vote up
def convert_result(self, result):
        if self.conversion_method == "none" or "result" not in result:
            return result
        try:
            if self.conversion_method == "text":
                result["result"] = str(result["result"])
            elif self.conversion_method == "json":
                result["result"] = loads(result["result"])
            elif self.conversion_method == "xml":
                result["result"] = parse(result["result"])
        except (ExpatError, JSONDecodeError) as exc:
            result = {
                "success": False,
                "text_response": result,
                "error": f"Conversion to {self.conversion_method} failed",
                "exception": str(exc),
            }
        return result 
Example #3
Source File: http.py    From mautrix-python with Mozilla Public License 2.0 6 votes vote down vote up
def _send(self, method: Method, endpoint: str, content: Union[bytes, str],
                    query_params: Dict[str, str], headers: Dict[str, str]) -> JSON:
        while True:
            request = self.session.request(str(method), endpoint, data=content,
                                           params=query_params, headers=headers)
            async with request as response:
                if response.status < 200 or response.status >= 300:
                    errcode = message = None
                    try:
                        response_data = await response.json()
                        errcode = response_data["errcode"]
                        message = response_data["error"]
                    except (JSONDecodeError, ContentTypeError, KeyError):
                        pass
                    raise make_request_error(http_status=response.status,
                                             text=await response.text(),
                                             errcode=errcode, message=message)

                if response.status == 429:
                    resp = await response.json()
                    await asyncio.sleep(resp["retry_after_ms"] / 1000, loop=self.loop)
                else:
                    return await response.json() 
Example #4
Source File: owlsim2.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_profile_ic(self, profile: List) -> Dict:
        """
        Given a list of individuals, return their information content
        """
        sim_response = get_attribute_information_profile(self.url, frozenset(profile))

        profile_ic = {}
        try:
            for cls in sim_response['input']:
                profile_ic[cls['id']] = cls['IC']
        except JSONDecodeError as json_exc:
            raise JSONDecodeError(
                "Cannot parse owlsim2 response: {}".format(json_exc.msg),
                json_exc.doc,
                json_exc.pos
            )

        return profile_ic 
Example #5
Source File: owlsim2.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def search(
            self,
            id_list: List,
            negated_classes: List,
            limit: Optional[int] = 100,
            method: Optional[SimAlgorithm] = SimAlgorithm.PHENODIGM) -> SimResult:
        """
        Owlsim2 search, calls search_by_attribute_set, and converts to SimResult object

        :raises JSONDecodeError: If the owlsim response is not valid json.
        """

        return self.filtered_search(
            id_list=id_list,
            negated_classes=negated_classes,
            limit=limit,
            taxon_filter=None,
            category_filter=None,
            method=method
        ) 
Example #6
Source File: jsonrpc_helper.py    From opensips-cli with GNU General Public License v3.0 6 votes vote down vote up
def get_reply(cmd):
    try:
        j = json.loads(cmd, object_pairs_hook=OrderedDict)
        if isinstance(j.get('error'), dict):
            raise JSONRPCError(j['error'].get('code', 500),
                               j['error'].get('message'),
                               j['error'].get('data'))

        elif 'result' not in j:
            raise JSONRPCError(-32603, 'Internal error')
        else:
            return j['result']
    except JSONDecodeError:
        raise JSONRPCException

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 
Example #7
Source File: owlsim2.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_attribute_information_profile(
        url: str,
        profile: Optional[FrozenSet[str]]=None,
        categories: Optional[FrozenSet[str]]=None) -> Dict:
    """
    Get the information content for a list of phenotypes
    and the annotation sufficiency simple and
    and categorical scores if categories are provied

    Ref: https://zenodo.org/record/834091#.W8ZnCxhlCV4
    Note that the simple score varies slightly from the pub in that
    it uses max_max_ic instead of mean_max_ic

    If no arguments are passed this function returns the
    system (loaded cohort) stats
    :raises JSONDecodeError: If the response body does not contain valid json.
    """
    owlsim_url = url + 'getAttributeInformationProfile'

    params = {
        'a': profile,
        'r': categories
    }
    return requests.post(owlsim_url, data=params, timeout=TIMEOUT).json() 
Example #8
Source File: owlsim2.py    From ontobio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def search_by_attribute_set(
        url: str,
        profile: FrozenSet[str],
        limit: Optional[int] = 100,
        namespace_filter: Optional[str]=None) -> Dict:
    """
    Given a list of phenotypes, returns a ranked list of individuals
    individuals can be filtered by namespace, eg MONDO, MGI, HGNC
    :returns Dict with the structure: {
        'unresolved' : [...]
        'query_IRIs' : [...]
        'results': {...}
    }
    :raises JSONDecodeError: If the response body does not contain valid json.
    """
    owlsim_url = url + 'searchByAttributeSet'

    params = {
        'a': profile,
        'limit': limit,
        'target': namespace_filter
    }
    return requests.post(owlsim_url, data=params, timeout=TIMEOUT).json() 
Example #9
Source File: _base_connection.py    From pyquil with Apache License 2.0 6 votes vote down vote up
def parse_error(res: requests.Response) -> ApiError:
    """
    Every server error should contain a "status" field with a human readable explanation of
    what went wrong as well as a "error_type" field indicating the kind of error that can be mapped
    to a Python type.

    There's a fallback error UnknownError for other types of exceptions (network issues, api
    gateway problems, etc.)
    """
    try:
        body = res.json()
    except JSONDecodeError:
        raise UnknownApiError(res.text)

    if "error_type" not in body:
        raise UnknownApiError(str(body))

    error_type = body["error_type"]
    status = body["status"]

    if re.search(r"[0-9]+ qubits were requested, but the QVM is limited to [0-9]+ qubits.", status):
        return TooManyQubitsError(status)

    error_cls = error_mapping.get(error_type, UnknownApiError)
    return error_cls(status) 
Example #10
Source File: jobs.py    From asgard-api with MIT License 6 votes vote down vote up
def validate_input(handler):
    async def _wrapper(wrapper: RequestWrapper):
        try:
            req_body = await wrapper.http_request.json()
        except JSONDecodeError as e:
            return json_response(
                ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(),
                status=HTTPStatus.BAD_REQUEST,
            )

        try:
            job = ScheduledJob(**req_body)
        except ValidationError as e:
            return json_response(
                ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(),
                status=HTTPStatus.UNPROCESSABLE_ENTITY,
            )

        wrapper.types_registry.set(job)
        return await call_http_handler(wrapper.http_request, handler)

    return _wrapper 
Example #11
Source File: dataIO.py    From Discord-Selfbot with GNU General Public License v3.0 6 votes vote down vote up
def save_json(self, filename, data):
        """Atomically save a JSON file given a filename and a dictionary."""
        path, ext = splitext(filename)
        tmp_file = "{}.{}.tmp".format(path, randint(1000, 9999))
        with open(tmp_file, 'w', encoding='utf-8') as f:
            dump(data, f, indent=4,sort_keys=True,separators=(',',' : '))
        try:
            with open(tmp_file, 'r', encoding='utf-8') as f:
                data = load(f)
        except decoder.JSONDecodeError:
            print("Attempted to write file {} but JSON "
                                  "integrity check on tmp file has failed. "
                                  "The original file is unaltered."
                                  "".format(filename))
            return False
        except Exception as e:
            print('A issue has occured saving ' + filename + '.\n'
                  'Traceback:\n'
                  '{0} {1}'.format(str(e), e.args))
            return False

        replace(tmp_file, filename)
        return True 
Example #12
Source File: handler.py    From webssh with MIT License 6 votes vote down vote up
def on_message(self, message):
        logging.debug('{!r} from {}:{}'.format(message, *self.src_addr))
        worker = self.worker_ref()
        try:
            msg = json.loads(message)
        except JSONDecodeError:
            return

        if not isinstance(msg, dict):
            return

        resize = msg.get('resize')
        if resize and len(resize) == 2:
            try:
                worker.chan.resize_pty(*resize)
            except (TypeError, struct.error, paramiko.SSHException):
                pass

        data = msg.get('data')
        if data and isinstance(data, UnicodeType):
            worker.data_to_dst.append(data)
            worker.on_write() 
Example #13
Source File: __main__.py    From ffmpeg-normalize with MIT License 6 votes vote down vote up
def _split_options(opts):
    """
    Parse extra options (input or output) into a list
    """
    if not opts:
        return []
    try:
        if opts.startswith('['):
            try:
                ret = [str(s) for s in json.loads(opts)]
            except JSONDecodeError as e:
                ret = shlex.split(opts)
        else:
            ret = shlex.split(opts)
    except Exception as e:
        raise FFmpegNormalizeError(
            "Could not parse extra_options: {}".format(e)
        )
    return ret 
Example #14
Source File: __init__.py    From checkov with Apache License 2.0 5 votes vote down vote up
def parse(filename):
    """
        Decode filename into an object
    """
    template = None
    template_lines = None
    try:
        (template, template_lines) = cfn_yaml.load(filename)
    except IOError as e:
        if e.errno == 2:
            LOGGER.error('Template file not found: %s', filename)
        elif e.errno == 21:
            LOGGER.error('Template references a directory, not a file: %s',
                         filename)
        elif e.errno == 13:
            LOGGER.error('Permission denied when accessing template file: %s',
                         filename)
    except UnicodeDecodeError as err:
        LOGGER.error('Cannot read file contents: %s', filename)
    except cfn_yaml.CfnParseError as err:
        pass
    except ScannerError as err:
        if err.problem in [
            'found character \'\\t\' that cannot start any token',
            'found unknown escape character']:
            try:
                (template, template_lines) = cfn_json.load(filename)
            except cfn_json.JSONDecodeError:
                pass
            except JSONDecodeError:
                pass
            except Exception as json_err:  # pylint: disable=W0703
                LOGGER.error(
                    'Template %s is malformed: %s', filename, err.problem)
                LOGGER.error('Tried to parse %s as JSON but got error: %s',
                             filename, str(json_err))
    except YAMLError as err:
        pass

    return template, template_lines 
Example #15
Source File: test_salesforce_stages.py    From datacollector-tests with Apache License 2.0 5 votes vote down vote up
def disable_cdc(client, subscription_id):
    """Utility method to disable Change Data Capture for Contact change events in an org.

    Args:
        client (:py:class:`simple_salesforce.Salesforce`): Salesforce client
        subscription_id (:obj:`str`) Event channel Id
    """
    try:
        client.restful(f'tooling/sobjects/PlatformEventChannelMember/{subscription_id}', method='DELETE')
    except JSONDecodeError:
        # Simple Salesforce issue #327
        # https://github.com/simple-salesforce/simple-salesforce/issues/327
        pass 
Example #16
Source File: test_encoders.py    From scrapbook with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_json_encode_failures(test_input):
    with pytest.raises(JSONDecodeError):
        JsonEncoder().encode(test_input) 
Example #17
Source File: users.py    From asgard-api with MIT License 5 votes vote down vote up
def update_user_partial(user_id: str, wrapper: RequestWrapper):

    try:
        body_data = await wrapper.http_request.json()
    except JSONDecodeError as e:
        return web.json_response(
            ErrorResource(errors=[ErrorDetail(msg=str(e))]).dict(),
            status=HTTPStatus.UNPROCESSABLE_ENTITY,
        )

    user = await UsersService.get_user_by_id(int(user_id), UsersBackend())
    status_code = HTTPStatus.ACCEPTED if user else HTTPStatus.NOT_FOUND

    try:
        if user:
            body_user = User(**body_data)
            user.name = body_user.name if body_user.name else user.name
            user.email = body_user.email if body_user.email else user.email
            updated_user = await UsersService.update_user(user, UsersBackend())
    except DuplicateEntity as de:
        return web.json_response(
            ErrorResource(errors=[ErrorDetail(msg=str(de))]).dict(),
            status=status_code,
        )

    return web.json_response(
        UserResource(user=updated_user).dict(), status=status_code
    ) 
Example #18
Source File: preview.py    From ontask_b with MIT License 5 votes vote down vote up
def _check_json_is_correct(text_content: str) -> bool:
    """Check the given string is a correct JSON object.

    :param text_content: String to consider
    :return: Boolean stating correctness
    """
    try:
        json.loads(text_content)
    except JSONDecodeError:
        return False
    return True 
Example #19
Source File: owlsim2.py    From ontobio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_owlsim_stats(url) -> Tuple[IcStatistic, Dict[str, IcStatistic]]:
    """
    :return Tuple[IcStatistic, Dict[str, IcStatistic]]
    :raises JSONDecodeError: If the response body does not contain valid json
    """
    scigraph = OntologyFactory().create('scigraph:ontology')
    category_stats = {}
    categories = [enum.value for enum in HpoUpperLevel]
    sim_response = get_attribute_information_profile(url, categories=tuple(categories))

    try:
        global_stats = IcStatistic(
            mean_mean_ic=float(sim_response['system_stats']['meanMeanIC']),
            mean_sum_ic=float(sim_response['system_stats']['meanSumIC']),
            mean_cls=float(sim_response['system_stats']['meanN']),
            max_max_ic=float(sim_response['system_stats']['maxMaxIC']),
            max_sum_ic=float(sim_response['system_stats']['maxSumIC']),
            individual_count=int(sim_response['system_stats']['individuals']),
            mean_max_ic=float(sim_response['system_stats']['meanMaxIC'])
        )
        for cat_stat in sim_response['categorical_scores']:
            category_stats[cat_stat['id']] = IcStatistic(
                mean_mean_ic=float(cat_stat['system_stats']['meanMeanIC']),
                mean_sum_ic=float(cat_stat['system_stats']['meanSumIC']),
                mean_cls=float(cat_stat['system_stats']['meanN']),
                max_max_ic=float(cat_stat['system_stats']['maxMaxIC']),
                max_sum_ic=float(cat_stat['system_stats']['maxSumIC']),
                individual_count=int(cat_stat['system_stats']['individuals']),
                mean_max_ic=float(cat_stat['system_stats']['meanMaxIC']),
                descendants=scigraph.descendants(cat_stat['id'], relations=["subClassOf"])
            )

    except JSONDecodeError as json_exc:
        raise JSONDecodeError(
            "Cannot parse owlsim2 response: {}".format(json_exc.msg),
            json_exc.doc,
            json_exc.pos
        )

    return global_stats, category_stats 
Example #20
Source File: utils.py    From pyxform with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_pyobj_from_json(str_or_path):
    """
    This function takes either a json string or a path to a json file,
    it loads the json into memory and returns the corresponding Python
    object.
    """
    try:
        # see if treating str_or_path as a path works
        fp = codecs.open(str_or_path, mode="r", encoding="utf-8")
        doc = json.load(fp, encoding="utf-8")
    except (IOError, JSONDecodeError, OSError):
        # if it doesn't work load the text
        doc = json.loads(str_or_path)
    return doc 
Example #21
Source File: client.py    From momoapi-python with MIT License 5 votes vote down vote up
def interpret_response(self, resp):
        rcode = resp.status_code
        rheaders = resp.headers

        try:
            rbody = resp.json()
        except JSONDecodeError:
            rbody = resp.text
            resp = Response(rbody, rcode, rheaders)

        if not (200 <= rcode < 300):
            self.handle_error_response(rbody, rcode, resp.text, rheaders)

        return resp 
Example #22
Source File: test_connectedapp.py    From CumulusCI with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_process_json_output_invalid(self):
        """ _process_json_output with invalid input logs output and raises JSONDecodeError """
        task = CreateConnectedApp(self.project_config, self.task_config)
        with pytest.raises(JSONDecodeError):
            task._process_json_output("invalid")
        self.assertEqual(
            self.task_log["error"], ["Failed to parse json from output: invalid"]
        ) 
Example #23
Source File: configuration.py    From cauldron with MIT License 5 votes vote down vote up
def load(self, source_path: str = None) -> 'Configuration':
        """..."""
        path = source_path if source_path else self._source_path
        if not os.path.exists(path):
            self._persistent = {}
            return self

        try:
            with open(path, 'r') as f:
                contents = f.read()
            if contents:
                self._persistent = json.loads(contents)
        except json_decoder.JSONDecodeError as err:
            if self._persistent == self.NO_VALUE:
                return self

            self._persistent = self.NO_VALUE
            log(
                """
                [ERROR]: Failed to decode json file
                  PATH: {path}
                  INFO: {msg}
                    LINE: {line}
                    CHAR: {char}
                """.format(
                    path=path,
                    msg=err.msg,
                    line=err.lineno,
                    char=err.colno
                )
            )
        return self 
Example #24
Source File: palette.py    From lighthouse with MIT License 5 votes vote down vote up
def _load_theme(self, filepath):
        """
        Load and apply the Lighthouse theme at the given filepath.
        """

        # attempt to read json theme from disk
        try:
            theme = self._read_theme(filepath)

        # reading file from dsik failed
        except OSError:
            lmsg("Could not open theme file at '%s'" % filepath)
            return False

        # JSON decoding failed
        except JSONDecodeError as e:
            lmsg("Failed to decode theme '%s' to json" % filepath)
            lmsg(" - " + str(e))
            return False

        # do some basic sanity checking on the given theme file
        if not self._validate_theme(theme):
            return False

        # try applying the loaded theme to Lighthouse
        try:
            self._apply_theme(theme)
        except Exception as e:
            lmsg("Failed to load Lighthouse user theme\n%s" % e)
            return False

        # return success
        self._notify_theme_changed()
        return True 
Example #25
Source File: crawler_selenium.py    From Price-monitor with GNU General Public License v3.0 5 votes vote down vote up
def get_huihui_item(self, item_id):
        huihui_info_dict = {"max_price": None, "min_price": None}
        url = 'https://zhushou.huihui.cn/productSense?phu=https://item.jd.com/' + item_id + '.html'
        try:
            self.chrome.get(url)
            # 共30秒
            retry = 15
            while retry:
                try:
                    element = self.chrome.find_element_by_tag_name('body').text
                    if element:
                        logging.info('Found body element: {}'.format(element))
                        break
                    else:
                        logging.info("huihui body元素出现,内容未出现重试2秒")
                        time.sleep(2)
                        retry -= 1
                except NoSuchElementException:
                    time.sleep(2)
                    retry -= 1
                except StaleElementReferenceException:
                    time.sleep(2)
                    retry -= 1
            url_text = self.chrome.find_element_by_tag_name('body').text
            info = json.loads(url_text)
            huihui_info_dict = {"max_price": info['max'], "min_price": info['min']}
            logging.info(huihui_info_dict)
        except decoder.JSONDecodeError as e:
            logging.warning('Crawl failure: {}'.format(e.msg))
        except NoSuchElementException as e:
            logging.warning('Crawl failure: {}'.format(e.msg))
        except TimeoutException as e:
            logging.warning('Crawl failure: {}'.format(e.msg))
        return huihui_info_dict 
Example #26
Source File: client.py    From detectlanguage-python with MIT License 5 votes vote down vote up
def handle_http_error(self, r, err):
		try:
			json = r.json()

			if not 'error' in json:
				raise DetectLanguageError(err)

			raise DetectLanguageError(json['error']['message'])
		except JSONDecodeError:
			raise DetectLanguageError(err) 
Example #27
Source File: client.py    From detectlanguage-python with MIT License 5 votes vote down vote up
def handle_response(self, r):
		try:
			r.raise_for_status()

			return r.json()
		except HTTPError as err:
			self.handle_http_error(r, err)
		except JSONDecodeError:
			raise DetectLanguageError("Error decoding response JSON") 
Example #28
Source File: phishing.py    From raw-packet with MIT License 5 votes vote down vote up
def do_POST(self):
        form: str = self.path
        if 'Content-Length' not in self.headers:
            self.error_NeedContentLegth()
        try:
            post_data: str = self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')
            post_data: Dict = loads(post_data)

            if form == '/check_username':
                response: bytes = bytes(post_data['username'], 'utf-8')
                self.send_response(200)
                self.send_header('Content-Type', 'text/plain; charset=UTF-8')
                self.send_header('Content-Length', str(len(response)))
                self.send_header('Connection', 'close')
                self.end_headers()
                self.wfile.write(response)

            elif form == '/check_credentials':
                self.server.base.print_success('Phishing success!'
                                               ' Address: ', self.address_string(),
                                               ' Username: ', post_data['username'],
                                               ' Password: ', post_data['password'])
                self.error_CheckCreds()

            else:
                self.error_FileNotFound()

        except decoder.JSONDecodeError:
            self.error_BadRequest()

        except UnicodeDecodeError:
            self.error_BadRequest()

        except KeyError:
            self.error_CheckCreds()

        except UnboundLocalError:
            self.error_CheckCreds()
    # endregion

    # region HEAD request 
Example #29
Source File: helpers.py    From Varken with MIT License 5 votes vote down vote up
def connection_handler(session, request, verify, as_is_reply=False):
    air = as_is_reply
    s = session
    r = request
    v = verify
    return_json = False

    disable_warnings(InsecureRequestWarning)

    try:
        get = s.send(r, verify=v)
        if get.status_code == 401:
            if 'NoSiteContext' in str(get.content):
                logger.info('Your Site is incorrect for %s', r.url)
            elif 'LoginRequired' in str(get.content):
                logger.info('Your login credentials are incorrect for %s', r.url)
            else:
                logger.info('Your api key is incorrect for %s', r.url)
        elif get.status_code == 404:
            logger.info('This url doesnt even resolve: %s', r.url)
        elif get.status_code == 200:
            try:
                return_json = get.json()
            except JSONDecodeError:
                logger.error('No JSON response. Response is: %s', get.text)
        if air:
            return get
    except InvalidSchema:
        logger.error("You added http(s):// in the config file. Don't do that.")
    except SSLError as e:
        logger.error('Either your host is unreachable or you have an SSL issue. : %s', e)
    except ConnectionError as e:
        logger.error('Cannot resolve the url/ip/port. Check connectivity. Error: %s', e)
    except ChunkedEncodingError as e:
        logger.error('Broken connection during request... oops? Error: %s', e)

    return return_json 
Example #30
Source File: cli.py    From pipenv with MIT License 5 votes vote down vote up
def review(full_report, bare, file):
    if full_report and bare:
        click.secho("Can't choose both --bare and --full-report/--short-report", fg="red")
        sys.exit(-1)

    try:
        input_vulns = read_vulnerabilities(file)
    except JSONDecodeError:
        click.secho("Not a valid JSON file", fg="red")
        sys.exit(-1)

    vulns = safety.review(input_vulns)
    output_report = report(vulns=vulns, full=full_report, bare_report=bare)
    click.secho(output_report, nl=False if bare and not vulns else True)