@@ -53,11 +53,8 @@ pub enum MatrixJsonBodyRejection {
53
53
#[ error( "Failed to read request body" ) ]
54
54
BytesRejection ( #[ from] BytesRejection ) ,
55
55
56
- #[ error( "Body is not a valid JSON document" ) ]
57
- NotJson ( #[ source] serde_json:: Error ) ,
58
-
59
- #[ error( "Request body has invalid parameters" ) ]
60
- BadJson ( #[ source] serde_json:: Error ) ,
56
+ #[ error( "Invalid JSON document" ) ]
57
+ Json ( #[ from] serde_json:: Error ) ,
61
58
}
62
59
63
60
impl IntoResponse for MatrixJsonBodyRejection {
@@ -92,15 +89,15 @@ impl IntoResponse for MatrixJsonBodyRejection {
92
89
status : StatusCode :: BAD_REQUEST ,
93
90
} ,
94
91
95
- Self :: NotJson ( _ ) => MatrixError {
96
- errcode : "M_NOT_JSON " ,
97
- error : "Body is not a valid JSON document " ,
92
+ Self :: Json ( err ) if err . is_data ( ) => MatrixError {
93
+ errcode : "M_BAD_JSON " ,
94
+ error : "JSON fields are not valid" ,
98
95
status : StatusCode :: BAD_REQUEST ,
99
96
} ,
100
97
101
- Self :: BadJson ( _) => MatrixError {
102
- errcode : "M_BAD_JSON " ,
103
- error : "JSON fields are not valid" ,
98
+ Self :: Json ( _) => MatrixError {
99
+ errcode : "M_NOT_JSON " ,
100
+ error : "Body is not a valid JSON document " ,
104
101
status : StatusCode :: BAD_REQUEST ,
105
102
} ,
106
103
} ;
@@ -138,11 +135,8 @@ where
138
135
139
136
let bytes = Bytes :: from_request ( req, state) . await ?;
140
137
141
- // We first parse it as a JSON value so that we can distinguish between
142
- // invalid JSON documents and invalid requests
143
- let value: serde_json:: Value =
144
- serde_json:: from_slice ( & bytes) . map_err ( MatrixJsonBodyRejection :: NotJson ) ?;
145
- let value = serde_json:: from_value ( value) . map_err ( MatrixJsonBodyRejection :: BadJson ) ?;
138
+ let value: T = serde_json:: from_slice ( & bytes) ?;
139
+
146
140
Ok ( Self ( value) )
147
141
}
148
142
}
0 commit comments