File tree Expand file tree Collapse file tree 1 file changed +8
-1
lines changed
Expand file tree Collapse file tree 1 file changed +8
-1
lines changed Original file line number Diff line number Diff line change @@ -181,11 +181,18 @@ export class HttpXhrBackend implements HttpBackend {
181181 // Check whether the body needs to be parsed as JSON (in many cases the browser
182182 // will have done that already).
183183 if ( req . responseType === 'json' && typeof body === 'string' ) {
184- // Attempt the parse. If it fails, a parse error should be delivered to the user.
184+ // Save the original body, before attempting XSSI prefix stripping.
185+ const originalBody = body ;
185186 body = body . replace ( XSSI_PREFIX , '' ) ;
186187 try {
188+ // Attempt the parse. If it fails, a parse error should be delivered to the user.
187189 body = body !== '' ? JSON . parse ( body ) : null ;
188190 } catch ( error ) {
191+ // Since the JSON.parse failed, it's reasonable to assume this might not have been a
192+ // JSON response. Restore the original body (including any XSSI prefix) to deliver
193+ // a better error response.
194+ body = originalBody ;
195+
189196 // If this was an error request to begin with, leave it as a string, it probably
190197 // just isn't JSON. Otherwise, deliver the parsing error to the user.
191198 if ( ok ) {
You can’t perform that action at this time.
0 commit comments