Skip to content

Commit

Permalink
Populate Text(58) field when rejectMessageOnUnhandledException=true
Browse files Browse the repository at this point in the history
If rejectMessageOnUnhandledException=true and an unhandled exception is
thrown from the fromCallback(), include the exception message in the
Text(58) field of the resulting Reject or BusinessMessageReject.
  • Loading branch information
mgatny committed Aug 2, 2024
1 parent d453c68 commit ca0e9d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 13 additions & 3 deletions quickfixj-core/src/main/java/quickfix/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1214,13 +1214,13 @@ ignore the message and let the problem correct itself (optimistic approach).
}
if (!(MessageUtils.isAdminMessage(msgType))
&& (sessionBeginString.compareTo(FixVersions.BEGINSTRING_FIX42) >= 0)) {
generateBusinessReject(message, BusinessRejectReason.APPLICATION_NOT_AVAILABLE,
generateBusinessReject(message, t.getMessage(), BusinessRejectReason.APPLICATION_NOT_AVAILABLE,
0);
} else {
if (MsgType.LOGON.equals(msgType)) {
disconnect("Problem processing Logon message", true);
} else {
generateReject(message, SessionRejectReason.OTHER, 0);
generateReject(message, t.getMessage(), SessionRejectReason.OTHER, 0);
}
}
} else {
Expand Down Expand Up @@ -1732,6 +1732,11 @@ private void setRejectReason(Message reject, int field, String reason,

private void generateBusinessReject(Message message, int err, int field) throws FieldNotFound,
IOException {
generateBusinessReject(message, null, err, field);
}

private void generateBusinessReject(Message message, String text, int err, int field) throws FieldNotFound,
IOException {
final Header header = message.getHeader();
ApplVerID targetDefaultApplicationVersionID = getTargetDefaultApplicationVersionID();
final Message reject = messageFactory.create(sessionID.getBeginString(), targetDefaultApplicationVersionID,
Expand All @@ -1746,7 +1751,12 @@ private void generateBusinessReject(Message message, int err, int field) throws
reject.setInt(BusinessRejectReason.FIELD, err);
state.incrNextTargetMsgSeqNum();

final String reason = BusinessRejectReasonText.getMessage(err);
final String reason;
if (text != null) {
reason = text;
} else {
reason = BusinessRejectReasonText.getMessage(err);
}
setRejectReason(reject, field, reason, field != 0);
getLog().onErrorEvent(
"Reject sent for message number " + msgSeqNum + (reason != null ? (": " + reason) : "")
Expand Down
8 changes: 7 additions & 1 deletion quickfixj-core/src/test/java/quickfix/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ public void fromApp(Message message, SessionID sessionId)
throws FieldNotFound, IncorrectDataFormat,
IncorrectTagValue, UnsupportedMessageType {
super.fromApp(message, sessionId);
throw new Error("TEST");
throw new Error("TESTApp");
}
};

Expand All @@ -1788,6 +1788,8 @@ public void fromApp(Message message, SessionID sessionId)
.getHeader().getString(MsgType.FIELD));
assertEquals(MsgType.BUSINESS_MESSAGE_REJECT, application
.lastToAppMessage().getHeader().getString(MsgType.FIELD));
assertEquals("TESTApp", application.lastToAppMessage()
.getString(Text.FIELD));

session.next(createHeartbeatMessage(3));
assertEquals(4, session.getExpectedTargetNum());
Expand All @@ -1796,6 +1798,8 @@ public void fromApp(Message message, SessionID sessionId)
.getHeader().getString(MsgType.FIELD));
assertEquals(MsgType.REJECT, application.lastToAdminMessage()
.getHeader().getString(MsgType.FIELD));
assertEquals("TESTAdmin", application.lastToAdminMessage()
.getString(Text.FIELD));

session.next(createAdminMessage(4));
assertEquals(5, session.getExpectedTargetNum());
Expand All @@ -1805,6 +1809,8 @@ public void fromApp(Message message, SessionID sessionId)
.getString(MsgType.FIELD));
assertEquals(MsgType.HEARTBEAT, application.lastToAdminMessage()
.getHeader().getString(MsgType.FIELD));
assertFalse(application.lastToAdminMessage()
.isSetField(Text.FIELD));
} catch (final Throwable t) {
fail("Error was thrown: " + t.getMessage());
}
Expand Down

0 comments on commit ca0e9d8

Please sign in to comment.