-
Notifications
You must be signed in to change notification settings - Fork 188
Description
Hello
In our logs we see several times JsonExceptions (Syntax error) while sending data via the REST Api. In some cases we messed up the request data and in some cases the response from the REST API is containing invalid data.
For us this is causing difficulties for us as one public method (e.g. Fulfillment::save()) is able to throw in 2 different places JsonException. For us it is impossible to act here with code.
For example: We would like to build an automated retry feature if the response body had an invalid response. If the request body conatains the syntax error we do not need to retry and would like to to notify our developers instead to fix the issue.
So here our suggestion to quickly enable everyone to determine if it was a response triggered JsonException while keeping it backward compatible:
- Build a new class named ResponseJsonException extending the original JsonException. Add an additional field for the plain text response body.
- Wrap the following line with try/catch block
return $responseBody ? json_decode($responseBody, true, 512, JSON_THROW_ON_ERROR) : null; - In the catch block: simply throw the new ResponseJsonException with same message as the caught JsonException, add the plain boday and add the caught JsonException as previous
This will make sure all existing projects stay working and everyone is able to determine if it was an Json error in the response. You can do this as well with the Request body if you like.
If you need more details, please don't hesitate to ask.