Skip to content

Commit 987ffa7

Browse files
committed
fix!: return Java API error codes as strings
1 parent 9a53e6b commit 987ffa7

5 files changed

Lines changed: 32 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.0.0] - 2026-07-17
9+
10+
### Changed
11+
12+
- Change `FacturapiException.getErrorCode()` to return strings, including converted legacy numeric codes.
13+
814
## [1.3.0] - 2026-06-07
915

1016
### Added

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.facturapi</groupId>
77
<artifactId>facturapi-java</artifactId>
8-
<version>1.3.0</version>
8+
<version>2.0.0</version>
99
<name>facturapi-java</name>
1010
<description>Official Java SDK for Facturapi</description>
1111
<url>https://github.com/facturapi/facturapi-java</url>

src/main/java/io/facturapi/FacturapiException.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class FacturapiException extends RuntimeException {
99
private final int statusCode;
10-
private final Object errorCode;
10+
private final String errorCode;
1111
private final String errorPath;
1212
private final String errorLocation;
1313
private final JsonNode errors;
@@ -29,14 +29,14 @@ public FacturapiException(String message, Throwable cause) {
2929
this.headers = Collections.emptyMap();
3030
}
3131

32-
public FacturapiException(String message, int statusCode, Object errorCode, String errorPath) {
32+
public FacturapiException(String message, int statusCode, String errorCode, String errorPath) {
3333
this(message, statusCode, errorCode, errorPath, null, null, null, Collections.emptyMap());
3434
}
3535

3636
public FacturapiException(
3737
String message,
3838
int statusCode,
39-
Object errorCode,
39+
String errorCode,
4040
String errorPath,
4141
String errorLocation,
4242
JsonNode errors,
@@ -57,7 +57,7 @@ public int getStatusCode() {
5757
return statusCode;
5858
}
5959

60-
public Object getErrorCode() {
60+
public String getErrorCode() {
6161
return errorCode;
6262
}
6363

src/main/java/io/facturapi/http/FacturapiHttpClient.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private static JsonNode firstDefined(JsonNode node, String... keys) {
210210
private FacturapiException buildApiException(String bodyText, Response response) {
211211
int statusCode = response.code();
212212
int resolvedStatus = statusCode;
213-
Object errorCode = null;
213+
String errorCode = null;
214214
String errorPath = null;
215215
String errorLocation = null;
216216
JsonNode errors = null;
@@ -246,17 +246,7 @@ private FacturapiException buildApiException(String bodyText, Response response)
246246

247247
JsonNode codeNode = firstDefined(root, "code");
248248
if (codeNode != null && !codeNode.isNull()) {
249-
if (codeNode.isTextual()) {
250-
errorCode = codeNode.asText();
251-
} else if (codeNode.isIntegralNumber()) {
252-
errorCode = codeNode.intValue();
253-
} else if (codeNode.isNumber()) {
254-
errorCode = codeNode.numberValue();
255-
} else if (codeNode.isBoolean()) {
256-
errorCode = codeNode.asBoolean();
257-
} else {
258-
errorCode = codeNode.toString();
259-
}
249+
errorCode = codeNode.isValueNode() ? codeNode.asText() : codeNode.toString();
260250
}
261251

262252
JsonNode pathNode = firstDefined(root, "path");

src/test/java/io/facturapi/FacturapiHttpClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,23 @@ void throwsFacturapiExceptionWithApiMessage() {
7777
assertEquals("3", ex.getHeaders().get("Retry-After").get(0));
7878
assertEquals("log_123", ex.getHeaders().get("x-facturapi-log-id").get(0));
7979
}
80+
81+
@Test
82+
void convertsNumericApiErrorCodesToStrings() {
83+
StubHttpClient httpClient = new StubHttpClient();
84+
httpClient.enqueueJson(400, "{\"message\":\"Invalid customer\",\"code\":400}");
85+
86+
FacturapiHttpClient client = new FacturapiHttpClient(
87+
FacturapiConfig.builder("sk_test_123")
88+
.httpClient(httpClient.client())
89+
.build()
90+
);
91+
92+
FacturapiException ex = assertThrows(
93+
FacturapiException.class,
94+
() -> client.get("/customers/cus_1", null, GenericResponse.class)
95+
);
96+
97+
assertEquals("400", ex.getErrorCode());
98+
}
8099
}

0 commit comments

Comments
 (0)