python / python/cpython

JSONDecodeError verbose error mesage

未关闭
#121,479 25 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

Example is the best way to start:

import json

try:
    raw_json_dict = b"{'a': 1, {'b': 2 'c': 3}}"
    letter_to_index = json.loads(raw_json_dict)
except JSONDecodeError as error:
   print(error.verbose())

# Printing => verbose error:"'b': 2 'c': 3}"

The problem:
Currently, when getting a JSONDecodeError you can't fully understand where the issue is because of unindecative error message.
The message only gives you the location of the error but not what actually caused the error itself.
For example, when you try to json.loads() a response from a server and you don't know what the content will be.
If you got a JSONDecodeError and you didn't log or save the raw response fron the server, you won't be able to figure out what caused the error and fix it.

The solution
First, I am not here to propose one unique solution, there could be multiple possible solutions. I just want to present the simple one to show its impact.

The solution I proposed in the example makes use of new function inside the JSONDecodeError class.
The verbose function prints the actual characters in which the error occurred and print a range of chars around them to get a context of the error.
Furthermore, the function will be able to get a parameter which will defines the range of characters around the error to get back.

I would very like to hear your opinions of this feature.
Thanks!

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先查看 json.loads 和 JSONDecodeError,然后阅读现有的 issue 讨论,以了解尚未解决的设计选项。明确应支持的上下文和范围行为,并添加测试来展示针对格式错误 JSON 的预期详细输出。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。