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

API error shadowed by ClassCastException message if error is not a json object #1846

Open
notfinch opened this issue Aug 8, 2024 · 6 comments
Assignees
Labels

Comments

@notfinch
Copy link

notfinch commented Aug 8, 2024

Describe the bug

Using some stripebased payments, when an unsuccessful response is resived, the "error" field in the body may be a string instead of a correct json object in raw response
While parsing the error in the com.stripe.net.LiveStripeResponseGetter#handleApiError method, a ClassCastException error is thrown which is thrown out and is uninformative to the sdk consumer

To Reproduce

  1. Make any api call that returns a non json type error
  2. Get exception java.lang.ClassCastException: class com.google.gson.JsonPrimitive cannot be cast to class com.google.gson.JsonObject

Expected behavior

It is expected that any api call with an error will throw a StripeException or its child exception with a message from the api

Code snippets

No response

OS

all

Java version

21

stripe-java version

26.1.0

API version

2024-06-20

Additional context

try {
      JsonObject jsonObject =
          ApiResource.INTERNAL_GSON
              .fromJson(response.body(), JsonObject.class)
              .getAsJsonObject("error");
      error = ApiResource.deserializeStripeObject(jsonObject.toString(), StripeError.class, this);
    } catch (JsonSyntaxException e) {
      raiseMalformedJsonError(response.body(), response.code(), response.requestId(), e);
    }

The issue here getAsJsonObject("error")
I think that just need to add ClassCastException to the catch block

@notfinch notfinch added the bug label Aug 8, 2024
@remi-stripe
Copy link
Contributor

@notfinch it should not be possible for the error to be a string instead of a proper JSON object. If this happens this would be a bug on our end we'd need to fix. I have definitely seen us do this incorrectly in the past but we've always quickly fixed those when it happened.

Do you have a concrete example where this happened and what the raw response looked like?

@remi-stripe remi-stripe self-assigned this Aug 8, 2024
@omkarkulkarni2704
Copy link

To fix the issue, add a ClassCastException catch block to handle cases where the error field is a string instead of a JsonObject and throw an informative StripeException.

@xavdid-stripe
Copy link
Member

@omkarkulkarni2704 were you seeing this issue as well? Do you (or anyone else) have a request ID to share so we can take a closer look?

@omkarkulkarni2704
Copy link

@xavdid-stripe Yes, I encountered this issue but I don’t have a specificrequest_id to share. The problem occurs when the error field is returned as a string instead of a JSON object, leading to a ClassCastException.
Let me know how I can assist further!

@xavdid-stripe
Copy link
Member

Right, but that really shouldn't happen.

If you notice it again, please note the request id and pass it along here! that'll help us figure out what's up

@omkarkulkarni2704
Copy link

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants