python / python/cpython

JSONDecodeError verbose error mesage

Aperta
#121,479 25 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando json.loads e JSONDecodeError, quindi leggi la discussione esistente sull’issue per comprendere le opzioni di progettazione ancora irrisolte. Definisci quale comportamento relativo al contesto e all’intervallo deve essere supportato e aggiungi test che dimostrino l’output dettagliato previsto per JSON non valido.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.