Your error handling approach looks good! Here are some suggestions to improve it and make the error data available dynamically in your BPMN error expression:
Current Code Analysis 
Your basic error handling structure is correct, but you can enhance it to pass the dynamic error details you want.
Enhanced Version with Dynamic Error Data
catch (HttpClientErrorException e) {
if (e.getStatusCode().is4xxClientError()) {
String responseBody = e.getResponseBodyAsString();
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
System.err.println("Resource not found: " + responseBody);
// Include the response body so it can be accessed in errorExpression
throw new ConnectorException("404", "Got a 404 - " + responseBody);
} else if (e.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
System.err.println("Bad request: " + responseBody);
throw new ConnectorException("400", "Got a 400 - " + responseBody);
} else if (e.getStatusCode().value() == 401) {
System.err.println("Unauthorized: " + responseBody);
throw new ConnectorException("401", "Got a 401 - " + responseBody);
}
// Handle other 4xx errors generically
else {
throw new ConnectorException(String.valueOf(e.getStatusCode().value()),
"Client error " + e.getStatusCode().value() + " - " + responseBody);
}
}
throw e; // Re-throw if not specifically handled
} catch (HttpServerErrorException e) {
if (e.getStatusCode().is5xxServerError()) {
String responseBody = e.getResponseBodyAsString();
System.err.println("Server error: " + responseBody);
throw new ConnectorException("500", "Got a 500 - " + responseBody);
}
System.out.println(e.getStatusCode() + " statuscode");
throw e; // Re-throw if not specifically handled
}
Better Approach - Parse Response Body for Structured Errors
If your API returns structured error responses (JSON), you can parse them:
catch (HttpClientErrorException e) {
String responseBody = e.getResponseBodyAsString();
String errorCode = extractErrorCodeFromResponse(responseBody); // Your custom method
String errorMessage = extractErrorMessageFromResponse(responseBody); // Your custom method
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
throw new ConnectorException("404",
"Resource not found - ErrorCode: " + errorCode + ", Message: " + errorMessage);
} else if (e.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
throw new ConnectorException("400",
"Bad request - ErrorCode: " + errorCode + ", Message: " + errorMessage);
} else if (e.getStatusCode().value() == 401) {
throw new ConnectorException("401",
"Unauthorized - ErrorCode: " + errorCode + ", Message: " + errorMessage);
}
// ... other cases
}
// Helper methods to parse your API response
private String extractErrorCodeFromResponse(String responseBody) {
// Parse JSON response and extract your custom error code
// e.g., "TPLMPPERR014"
return parseErrorCode(responseBody);
}
private String extractErrorMessageFromResponse(String responseBody) {
// Parse JSON response and extract your custom error message
return parseErrorMessage(responseBody);
}
Updated Error Expression
Then in your BPMN error expression, you can extract the dynamic data from the error message:
if error.code = "400" then
bpmnError("400", "Got a 400", {
ErrorCode: substring(error.message, index of(error.message, "ErrorCode: ") + 11, index of(error.message, ", Message:")),
ErrorMessage: substring(error.message, index of(error.message, "Message: ") + 9)
})
else if error.code = "404" then
bpmnError("404", "Got a 404", {
ErrorCode: substring(error.message, index of(error.message, "ErrorCode: ") + 11, index of(error.message, ", Message:")),
ErrorMessage: substring(error.message, index of(error.message, "Message: ") + 9)
})
else if error.code = "500" then
bpmnError("500", "Got a 500", {
ErrorCode: substring(error.message, index of(error.message, "ErrorCode: ") + 11, index of(error.message, ", Message:")),
ErrorMessage: substring(error.message, index of(error.message, "Message: ") + 9)
})
else null
Improvements to Your Current Code:
- Add response body to error message so you can access the API’s error details
- Handle other 4xx errors generically instead of just the specific ones
- Extract structured error information from the API response if it’s JSON
- Consider logging levels - use a proper logger instead of
System.err.println
- Add null checks for response body
Alternative: JSON-based Approach
If you want cleaner parsing in FEEL, structure your error message as JSON:
// In your Java code
String errorJson = String.format(
"{\"httpCode\":\"%s\", \"errorCode\":\"%s\", \"errorMessage\":\"%s\"}",
e.getStatusCode().value(),
extractedErrorCode,
extractedErrorMessage
);
throw new ConnectorException(String.valueOf(e.getStatusCode().value()), errorJson);
Then parse it in FEEL using JSON functions.
Does this help you achieve the dynamic error handling you’re looking for?