Python json.JSONDecodeError() Examples
The following are 30
code examples of json.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
, or try the search function
.

Example #1
Source File: login.py From maubot with GNU Affero General Public License v3.0 | 7 votes |
def login(server, username, password, alias) -> None: data = { "username": username, "password": password, } try: with urlopen(f"{server}/_matrix/maubot/v1/auth/login", data=json.dumps(data).encode("utf-8")) as resp_data: resp = json.load(resp_data) config["servers"][server] = resp["token"] if not config["default_server"]: print(Fore.CYAN, "Setting", server, "as the default server") config["default_server"] = server if alias: config["aliases"][alias] = server save_config() print(Fore.GREEN + "Logged in successfully") except HTTPError as e: try: err = json.load(e) except json.JSONDecodeError: err = {} print(Fore.RED + err.get("error", str(e)) + Fore.RESET)
Example #2
Source File: huaban.py From PickTrue with MIT License | 6 votes |
def get(self, url, require_json=False, **kwargs): """ :param require_json: If require_json is True and the result is not json-encoded, will raise error then have a retry. :rtype: requests.Response """ if 'timeout' in kwargs: kwargs.pop('timeout') resp = self.session.get(url, timeout=(2, 30), **kwargs) if require_json: try: resp.json() except JSONDecodeError: download_logger.error( "Failed to convert resp to json for url {}: {}".format( url, resp.text, ) ) raise return resp
Example #3
Source File: base.py From tandem with Apache License 2.0 | 6 votes |
def handle_raw_data(self, retrieve_io_data): try: io_data = retrieve_io_data() if io_data is None or io_data.is_empty(): return data_as_dict = json.loads(io_data.get_data()) handled = self.handle_message(data_as_dict, io_data) if not handled: logging.info( "Protocol message was not handled because " "no handler was registered.", ) except json.JSONDecodeError: logging.info( "Protocol message was ignored because it was not valid JSON.", ) except: logging.exception("Exception when handling protocol message:") raise
Example #4
Source File: base.py From tandem with Apache License 2.0 | 6 votes |
def handle_raw_data(self, retrieve_io_data): try: io_data = retrieve_io_data() if io_data is None or io_data.is_empty(): return data_as_dict = json.loads(io_data.get_data()) handled = self.handle_message(data_as_dict, io_data) if not handled: logging.info( "Protocol message was not handled because " "no handler was registered.", ) except json.JSONDecodeError: logging.info( "Protocol message was ignored because it was not valid JSON.", ) except: logging.exception("Exception when handling protocol message:") raise
Example #5
Source File: base.py From tandem with Apache License 2.0 | 6 votes |
def handle_raw_data(self, retrieve_io_data): try: io_data = retrieve_io_data() if io_data is None or io_data.is_empty(): return data_as_dict = json.loads(io_data.get_data()) handled = self.handle_message(data_as_dict, io_data) if not handled: logging.info( "Protocol message was not handled because " "no handler was registered.", ) except json.JSONDecodeError: logging.info( "Protocol message was ignored because it was not valid JSON.", ) except: logging.exception("Exception when handling protocol message:") raise
Example #6
Source File: client_auth.py From maubot with GNU Affero General Public License v3.0 | 6 votes |
def read_client_auth_request(request: web.Request) -> Tuple[Optional[AuthRequestInfo], Optional[web.Response]]: server_name = request.match_info.get("server", None) server = registration_secrets().get(server_name, None) if not server: return None, resp.server_not_found try: body = await request.json() except JSONDecodeError: return None, resp.body_not_json try: username = body["username"] password = body["password"] except KeyError: return None, resp.username_or_password_missing try: base_url = server["url"] secret = server["secret"] except KeyError: return None, resp.invalid_server api = HTTPAPI(base_url, "", loop=get_loop()) user_type = body.get("user_type", "bot") return AuthRequestInfo(api, secret, username, password, user_type), None
Example #7
Source File: login.py From maubot with GNU Affero General Public License v3.0 | 6 votes |
def login(request: web.Request) -> web.Response: try: data = await request.json() except json.JSONDecodeError: return resp.body_not_json secret = data.get("secret") if secret and get_config()["server.unshared_secret"] == secret: user = data.get("user") or "root" return resp.logged_in(create_token(user)) username = data.get("username") password = data.get("password") if get_config().check_password(username, password): return resp.logged_in(create_token(username)) return resp.bad_auth
Example #8
Source File: manager.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 6 votes |
def update_manager_status(request: web.Request, params: Any) -> web.Response: log.info('MANAGER.UPDATE_MANAGER_STATUS (status:{}, force_kill:{})', params['status'], params['force_kill']) try: params = await request.json() status = params['status'] force_kill = params['force_kill'] except json.JSONDecodeError: raise InvalidAPIParameters(extra_msg='No request body!') except (AssertionError, ValueError) as e: raise InvalidAPIParameters(extra_msg=str(e.args[0])) if force_kill: await request.app['registry'].kill_all_sessions() await request.app['config_server'].update_manager_status(status) return web.Response(status=204)
Example #9
Source File: parser.py From linehaul with Apache License 2.0 | 6 votes |
def Pip6UserAgent(user_agent): # We're only concerned about pip user agents. if not user_agent.startswith("pip/"): raise UnableToParse # This format was brand new in pip 6.0, so we'll need to restrict it # to only versions of pip newer than that. version_str = user_agent.split()[0].split("/", 1)[1] version = packaging.version.parse(version_str) if version not in SpecifierSet(">=6", prereleases=True): raise UnableToParse try: return json.loads(user_agent.split(maxsplit=1)[1]) except (json.JSONDecodeError, UnicodeDecodeError, IndexError): raise UnableToParse from None
Example #10
Source File: connection.py From insightconnect-plugins with MIT License | 6 votes |
def get_alerts_by_key_value(self, key, value): self.update_access_token() self.update_time_from() full_endpoint_url = self.setup_alerts_endpoint() self.logger.info("Connecting to: " + full_endpoint_url) response = self.session.get(full_endpoint_url) self.raise_exception_on_error(response) matching_alerts = [] self.logger.info("Looking for {} matching {}".format(value, key)) try: matching_alerts = list(filter(lambda a: a.get(key) == value, response.json())) except json.JSONDecodeError as e: self.logger.error("Alerts returned were in an unexpected format!") raise e return komand.helper.clean(matching_alerts)
Example #11
Source File: repo_entries.py From wit with Apache License 2.0 | 6 votes |
def parse(text: str, path: Path, rev: str) -> List[RepoEntry]: try: fromtext = json.loads(text) except json.JSONDecodeError as e: print("Failed to parse json in {}:{}: {}".format(path, rev, e.msg)) sys.exit(1) entries = [] fmt = Format.from_path(path) if fmt is Format.Manifest: for entry in fromtext: entries.append(OriginalEntry.from_dict(entry)) if fmt is Format.Lock: for _, entry in fromtext.items(): entries.append(OriginalEntry.from_dict(entry)) # Check for duplicates names = [entry.checkout_path for entry in entries] if len(names) != len(set(names)): dup = set([x for x in names if names.count(x) > 1]) print("Two repositories have same checkout path in {}:{}: {}".format(path, rev, dup)) sys.exit(1) return entries
Example #12
Source File: sources_http_client.py From koku with GNU Affero General Public License v3.0 | 6 votes |
def build_status_header(self): """Build org-admin header for internal status delivery.""" try: encoded_auth_header = self._identity_header.get("x-rh-identity") identity = json.loads(b64decode(encoded_auth_header)) account = identity["identity"]["account_number"] identity_header = { "identity": { "account_number": account, "type": "User", "user": {"username": "cost-mgmt", "email": "cost-mgmt@redhat.com", "is_org_admin": True}, } } json_identity = json_dumps(identity_header) cost_internal_header = b64encode(json_identity.encode("utf-8")) return {"x-rh-identity": cost_internal_header} except (binascii.Error, json.JSONDecodeError, TypeError, KeyError) as error: LOG.error(f"Unable to build internal status header. Error: {str(error)}")
Example #13
Source File: test_cachito.py From atomic-reactor with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_request_sources_error(status_code, error, error_body, caplog): responses.add( responses.POST, '{}/api/v1/requests'.format(CACHITO_URL), content_type='application/json', body=error_body, status=status_code, ) with pytest.raises(error): CachitoAPI(CACHITO_URL).request_sources(CACHITO_REQUEST_REPO, CACHITO_REQUEST_REF) try: response_data = json.loads(error_body) except ValueError: # json.JSONDecodeError in py3 assert 'Cachito response' not in caplog.text else: response_json = 'Cachito response:\n{}'.format(json.dumps(response_data, indent=4)) # Since Python 3.7 logger adds additional whitespaces by default -> checking without them assert re.sub(r'\s+', " ", response_json) in re.sub(r'\s+', " ", caplog.text)
Example #14
Source File: fcoin_client.py From vnpy_crypto with MIT License | 6 votes |
def _onMessage(self, msg): try: data = json.loads(msg) except json.JSONDecodeError: # Something wrong with this data, log and discard return if isinstance(data, dict): if 'topics' in data: self._response_handler(data['topics']) elif 'type' in data: type = data.pop('type') self._data_handler(type, data) else: pass
Example #15
Source File: request.py From jumpserver-python-sdk with GNU General Public License v2.0 | 5 votes |
def clean_result(resp): if resp.status_code >= 500: msg = "Response code is {0.status_code}: {0.text}".format(resp) logger.error(msg) raise ResponseError(msg) try: _ = resp.json() except (json.JSONDecodeError, simplejson.scanner.JSONDecodeError): msg = "Response json couldn't be decode: {0.text}".format(resp) logger.error(msg) raise ResponseError(msg) else: return resp
Example #16
Source File: test_request.py From jumpserver-python-sdk with GNU General Public License v2.0 | 5 votes |
def test_clean_result_decode_error(self): resp_mock = mock.MagicMock() resp_mock.status_code = 200 resp_mock.json.side_effect = json.JSONDecodeError("error", "\n\n", 1) with self.assertRaises(ResponseError): self.http.clean_result(resp_mock)
Example #17
Source File: xq_client.py From backtrader-cn with GNU General Public License v3.0 | 5 votes |
def __get_create_cube_token(self): """获取创建组合时需要的token信息 :return: token """ response = self.session.get(self.config["get_token_url"], params={ "api": "/cubes/create.json" }) try: token_response = json.loads(response.text) except JSONDecodeError: raise TradeError("解析创建组合的token信息失败: %s" % response.text) if 'token' not in token_response: raise TradeError("获取创建组合的token信息失败: %s" % response.text) return token_response['token']
Example #18
Source File: xq_client.py From backtrader-cn with GNU General Public License v3.0 | 5 votes |
def get_cubes_list(self, type=4): """获取组合详情,默认获取自选组合 :param type: 组合名字的前缀, 1: 全部组合。 4: 我的组合。 5: 只看沪深组合。 6: 只看美股。7: 只看港股 :return: 组合列表 """ response = self.session.get(self.config['get_cubes_list'], params={ "category": 1, "type": type }) try: cubes_response = json.loads(response.text) except JSONDecodeError: log.warning(response.text) raise TradeError("解析组合列表失败: %s" % response.text) if 'stocks' not in cubes_response: log.warning(cubes_response) raise TradeError("获取组合信息失败: %s" % response.text) cubes_code_list = [cube['code'] for cube in cubes_response['stocks']] response = self.session.get(self.config['get_cubes_detail'], params={ "code": ','.join(cubes_code_list), "return_hasexist": False, }) try: cubes_detail_response = json.loads(response.text) except JSONDecodeError: raise TradeError("解析组合详情失败: %s" % response.text) if 'stocks' not in cubes_response: raise TradeError("获取组合信息失败: %s" % response.text) return cubes_detail_response
Example #19
Source File: base.py From drydock with Apache License 2.0 | 5 votes |
def req_json(self, req): if req.content_length is None or req.content_length == 0: return None if req.content_type is not None and req.content_type.lower( ) == 'application/json': raw_body = req.stream.read(req.content_length or 0) if raw_body is None: return None try: json_body = json.loads(raw_body.decode('utf-8')) return json_body except json.JSONDecodeError as jex: print( "Invalid JSON in request: \n%s" % raw_body.decode('utf-8')) self.error( req.context, "Invalid JSON in request: \n%s" % raw_body.decode('utf-8')) raise errors.InvalidFormat( "%s: Invalid JSON in body: %s" % (req.path, jex)) else: raise errors.InvalidFormat("Requires application/json payload")
Example #20
Source File: gquery.py From grlc with MIT License | 5 votes |
def get_yaml_decorators(rq): """ Returns the yaml decorator metadata only (this is needed by triple pattern fragments) """ # glogger.debug('Guessing decorators for query {}'.format(rq)) if not rq: return None yaml_string = '' query_string = '' if isinstance(rq, dict): # json query (sparql transformer) if 'grlc' in rq: yaml_string = rq['grlc'] query_string = rq else: # classic query yaml_string = "\n".join([row.lstrip('#+') for row in rq.split('\n') if row.startswith('#+')]) query_string = "\n".join([row for row in rq.split('\n') if not row.startswith('#+')]) query_metadata = None if type(yaml_string) == dict: query_metadata = yaml_string elif type(yaml_string) == str: try: # Invalid YAMLs will produce empty metadata query_metadata = yaml.load(yaml_string) except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e: try: query_metadata = json.loads(yaml_string) except json.JSONDecodeError: glogger.warning("Query decorators could not be parsed; check your YAML syntax") # If there is no YAML string if query_metadata is None: query_metadata = {} query_metadata['query'] = query_string # glogger.debug("Parsed query decorators: {}".format(query_metadata)) return query_metadata
Example #21
Source File: test_parse.py From jsonrpcclient with MIT License | 5 votes |
def test_parse_invalid_json(): with pytest.raises(JSONDecodeError): parse("{dodgy}", batch=False)
Example #22
Source File: fields.py From django-notifs with MIT License | 5 votes |
def from_db_value(self, value, expression, connection, *args, **kwargs): """Convert the JSON back to a dictionary.""" try: return json.loads(value) except json.JSONDecodeError: return value
Example #23
Source File: artstation.py From PickTrue with MIT License | 5 votes |
def request_url(self, url): text = self.server.requester.send_and_wait(url) try: return json.loads(text) except json.JSONDecodeError: return text
Example #24
Source File: config.py From controller with MIT License | 5 votes |
def set_tags(self, previous_config): """verify the tags exist on any nodes as labels""" if not self.tags: if settings.DEIS_DEFAULT_CONFIG_TAGS: try: tags = json.loads(settings.DEIS_DEFAULT_CONFIG_TAGS) self.tags = tags except json.JSONDecodeError as e: return else: return # Get all nodes with label selectors nodes = self._scheduler.node.get(labels=self.tags).json() if nodes['items']: return labels = ['{}={}'.format(key, value) for key, value in self.tags.items()] message = 'No nodes matched the provided labels: {}'.format(', '.join(labels)) # Find out if there are any other tags around old_tags = getattr(previous_config, 'tags') if old_tags: old = ['{}={}'.format(key, value) for key, value in old_tags.items()] new = set(labels) - set(old) if new: message += ' - Addition of {} is the cause'.format(', '.join(new)) raise DeisException(message)
Example #25
Source File: client.py From clashroyale with MIT License | 5 votes |
def _raise_for_status(self, resp, text, *, method=None): try: data = json.loads(text) except json.JSONDecodeError: data = text code = getattr(resp, 'status', None) or getattr(resp, 'status_code') log.debug(self.REQUEST_LOG.format(method=method or resp.request_info.method, url=resp.url, text=text, status=code)) if self.error_debug: raise ServerError(resp, data) if 300 > code >= 200: # Request was successful if self.using_cache: cached_data = { 'c_timestamp': datetime.utcnow().timestamp(), 'data': data } self.cache[str(resp.url)] = cached_data return data, False, datetime.utcnow(), resp # value, cached, last_updated, response if code == 400: raise BadRequest(resp, data) if code in (401, 403): # Unauthorized request - Invalid token raise Unauthorized(resp, data) if code == 404: # Tag not found raise NotFoundError(resp, data) if code == 429: raise RatelimitError(resp, data) if code == 503: # Maintainence raise ServerError(resp, data) raise UnexpectedError(resp, data)
Example #26
Source File: client.py From clashroyale with MIT License | 5 votes |
def _raise_for_status(self, resp, text, *, method=None): try: data = json.loads(text) except json.JSONDecodeError: data = text code = getattr(resp, 'status', None) or getattr(resp, 'status_code') log.debug(self.REQUEST_LOG.format(method=method or resp.request_info.method, url=resp.url, text=text, status=code)) if self.error_debug: raise ServerError(resp, data) if 300 > code >= 200: # Request was successful if self.using_cache: cached_data = { 'c_timestamp': datetime.utcnow().timestamp(), 'data': data } self.cache[str(resp.url)] = cached_data if resp.headers.get('x-ratelimit-limit'): self.ratelimit = [ int(resp.headers['x-ratelimit-limit']), int(resp.headers['x-ratelimit-remaining']), int(resp.headers.get('x-ratelimit-reset', 0)) ] return data, False, datetime.utcnow(), resp # value, cached, last_updated, response if code == 401: # Unauthorized request - Invalid token raise Unauthorized(resp, data) if code in (400, 404): # Tag not found raise NotFoundError(resp, data) if code == 417: raise NotTrackedError(resp, data) if code == 429: raise RatelimitError(resp, data) if code >= 500: # Something wrong with the api servers :( raise ServerError(resp, data) raise UnexpectedError(resp, data)
Example #27
Source File: test_keep_alive_timeout.py From sanic with MIT License | 5 votes |
def _local_request(self, method, url, *args, **kwargs): raw_cookies = kwargs.pop("raw_cookies", None) request_keepalive = kwargs.pop( "request_keepalive", CONFIG_FOR_TESTS["KEEP_ALIVE_TIMEOUT"] ) if not self._session: self._session = self.get_new_session() try: response = await getattr(self._session, method.lower())( url, timeout=request_keepalive, *args, **kwargs ) except NameError: raise Exception(response.status_code) try: response.json = response.json() except (JSONDecodeError, UnicodeDecodeError): response.json = None response.body = await response.aread() response.status = response.status_code response.content_type = response.headers.get("content-type") if raw_cookies: response.raw_cookies = {} for cookie in response.cookies: response.raw_cookies[cookie.name] = cookie return response
Example #28
Source File: testing.py From sanic with MIT License | 5 votes |
def _local_request(self, method, url, *args, **kwargs): logger.info(url) raw_cookies = kwargs.pop("raw_cookies", None) if method == "websocket": async with websockets.connect(url, *args, **kwargs) as websocket: websocket.opened = websocket.open return websocket else: async with self.get_new_session() as session: try: response = await getattr(session, method.lower())( url, *args, **kwargs ) except NameError: raise Exception(response.status_code) response.body = await response.aread() response.status = response.status_code response.content_type = response.headers.get("content-type") # response can be decoded as json after response._content # is set by response.aread() try: response.json = response.json() except (JSONDecodeError, UnicodeDecodeError): response.json = None if raw_cookies: response.raw_cookies = {} for cookie in response.cookies.jar: response.raw_cookies[cookie.name] = cookie return response
Example #29
Source File: settings.py From Twitch-Chat-Downloader with MIT License | 5 votes |
def load(self, filepath: str) -> Dict[str, Any]: """ Load dictionary from json file :param filepath: filepath to load from :return: Configuration dictionary """ # Create settings file from reference file if necessary if not self.filepath.exists(): self.directory.mkdir(parents=True, exist_ok=True) # Missing reference file if not self.reference_filepath.exists(): Logger().log(f'Could not find {self.reference_filepath}', Log.CRITICAL) exit(1) # Load config from reference settings with open(self.reference_filepath, 'r') as file: config = json.load(file) Settings.write(self.filepath, data=config) return config # Load from settings file try: with open(filepath, 'r') as file: return json.load(file) except json.JSONDecodeError: print('Invalid settings format') exit(1)
Example #30
Source File: parser.py From waspy with Apache License 2.0 | 5 votes |
def decode(self, data: bytes): if data: try: return json.loads(data) except json.JSONDecodeError: raise ParseError("Invalid JSON")