danielgtaylor / danielgtaylor/python-betterproto

Better exception messages

Aperta
#390 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
enhancement medium
Lingua principale
Python
Stelle
1.8k
Fork
234
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I think anyone who has used the library for a while will eventually run into an issue that is made hard to debug by the error messages when serialising and deserialising fails. Consider an error like:
```py
File "betterproto/__init__.py", line 767, in __bytes__
output += _serialize_single(
File "betterproto/__init__.py", line 359, in _serialize_single
value = _preprocess_single(proto_type, wraps, value)
File "betterproto/__init__.py", line 325, in _preprocess_single
return struct.pack(_pack_fmt(proto_type), value)
struct.error: argument out of range
```
The message is relatively unclear as to what has gone wrong especially as it does not include the field which was out of the range and the value that caused the error.

Ideally the error would tell you the name of the field that errored, the value and the value the range has to be in.
```py
File "betterproto/__init__.py", line 767, in __bytes__
output += _serialize_single(
File "betterproto/__init__.py", line 359, in _serialize_single
value = _preprocess_single(proto_type, wraps, value)
File "betterproto/__init__.py", line 325, in _preprocess_single
return struct.pack(_pack_fmt(proto_type), value)
struct.error: argument out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
File "betterproto/__init__.py", line 775, in __bytes__
raise SerialisationError(field, value) from e
SerialisationError: Failed to serialize field 'rtime32_start_time', with value 123456789123456789, value should be in [0, 2**32)
```
This obviously could be adapted for more errors than just the struct error, but it would be a good easy starting point and would incure no runtime cost in newer python versions.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia in betterproto/__init__.py, in __bytes__, _serialize_single e _preprocess_single, seguendo il fallimento di struct.pack mostrato nel report. Verifica come viene sollevato SerialisationError e fai in modo che il comportamento completato identifichi il campo, il suo valore e l’intervallo valido per gli errori di serializzazione dovuti a valori fuori intervallo.

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

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.