Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch IllegalStateException when attempting to parse wrong formatted product image jsons #11589

Open
JorgeMucientes opened this issue May 27, 2024 · 1 comment
Labels
priority: low Not many people are affected or there’s a workaround, etc. type: crash The worst kind of bug.

Comments

@JorgeMucientes
Copy link
Contributor

We are experiencing some crashes when attempting to parse the following json section from a product model:

    "images": [
      {
        "id": 795,
        "date_created": "2017-03-23T14:03:08",
        "date_created_gmt": "2017-03-23T20:03:08",
        "date_modified": "2017-03-23T14:03:08",
        "date_modified_gmt": "2017-03-23T20:03:08",
        "src": "https://example.com/wp-content/uploads/2017/03/T_4_front-11.jpg",
        "name": "",
        "alt": ""
      },
      {

The reason for the crash is because there seems to be some plugin (we don't know which one) that is modifying the structure of that json, returning something like this, instead of the json above:

{
   "15752":"https://passofino.com/wp-content/uploads/2024/03/pantofi-decupati-din-piele-yasmina-donna-0.jpg",
   "15754":"https://passofino.com/wp-content/uploads/2024/03/pantofi-decupati-din-piele-yasmina-donna-1.jpg",
   "15755":"https://passofino.com/wp-content/uploads/2024/03/pantofi-decupati-din-piele-yasmina-donna-2.jpg",
...
}

This causes the following code to crash, because we are only catching JsonParseException.

    fun getFirstImageUrl(): String? {
        try {
            if (images.isNotEmpty()) {
                Gson().fromJson(images, JsonElement::class.java).asJsonArray.firstOrNull { jsonElement ->
                    return (jsonElement.asJsonObject).getString("src")
                }
            }
        } catch (e: JsonParseException) {
            AppLog.e(T.API, e)
        }
        return null
    }

The idea of this issue is 2 things:

  • Catch the IllegalStateException as well
  • Log a Sentry error whenever a IllegalStateException or JsonParseException happen in this context, to keep track of when this is happening without making the app crash.
@JorgeMucientes JorgeMucientes added the type: enhancement A request for an enhancement. label May 27, 2024
@dangermattic
Copy link
Collaborator

dangermattic commented May 27, 2024

🚫 Please add a type label (e.g. type: enhancement) and a feature label (e.g. feature: stats) to this issue.

@JorgeMucientes JorgeMucientes added type: crash The worst kind of bug. priority: low Not many people are affected or there’s a workaround, etc. and removed type: enhancement A request for an enhancement. labels Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low Not many people are affected or there’s a workaround, etc. type: crash The worst kind of bug.
Projects
None yet
Development

No branches or pull requests

2 participants