2
2
from collections import namedtuple
3
3
4
4
from google .protobuf import json_format
5
+ from google .protobuf import message
5
6
from google .protobuf import symbol_database as _symbol_database
6
7
7
8
@@ -48,7 +49,13 @@ def _get_endpoint(self, path):
48
49
@staticmethod
49
50
def json_decoder (body , data_obj = None ):
50
51
data = data_obj ()
51
- json_format .Parse (body , data )
52
+ try :
53
+ json_format .Parse (body , data )
54
+ except json_format .ParseError as exc :
55
+ raise exceptions .TwirpServerException (
56
+ code = errors .Errors .Malformed ,
57
+ message = "the json request could not be decoded" ,
58
+ ) from exc
52
59
return data
53
60
54
61
@staticmethod
@@ -64,7 +71,13 @@ def json_encoder(value, data_obj=None):
64
71
@staticmethod
65
72
def proto_decoder (body , data_obj = None ):
66
73
data = data_obj ()
67
- data .ParseFromString (body )
74
+ try :
75
+ data .ParseFromString (body )
76
+ except message .DecodeError as exc :
77
+ raise exceptions .TwirpServerException (
78
+ code = errors .Errors .Malformed ,
79
+ message = "the protobuf request could not be decoded" ,
80
+ ) from exc
68
81
return data
69
82
70
83
@staticmethod
@@ -90,4 +103,4 @@ def _get_encoder_decoder(self, endpoint, headers):
90
103
code = errors .Errors .BadRoute ,
91
104
message = "unexpected Content-Type: " + ctype
92
105
)
93
- return encoder , decoder
106
+ return encoder , decoder
0 commit comments