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

未关闭
#1,846 8 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 3 天内回复

@remi-stripe 已经在做这个了。

开始于 2024年8月8日。

评估

这个 Issue 还没有评估数据。

描述

bug future
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

主要语言
Java
星标
1k
派生
412
平均合并
4 天 12 小时
30 天内合并 PR
16

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

stripe/stripe-java 的其他 Issue

查看 stripe/stripe-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。