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

Avoid exceptions when dumping ctx #624

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions jpos/src/main/java/org/jpos/transaction/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,12 @@ public synchronized Map<Object,Object> getMap() {
}

protected void dumpMap (PrintStream p, String indent) {
if (map != null) {
Map<Object,Object> cloned;
cloned = Collections.synchronizedMap (new LinkedHashMap<>());
synchronized(map) {
cloned.putAll(map);
}
cloned.entrySet().forEach(e -> dumpEntry(p, indent, e));
Map<Object,Object> m = getMap();
Map<Object,Object> cloned = Collections.synchronizedMap (new LinkedHashMap<>());
synchronized(m) {
cloned.putAll(m);
}
cloned.entrySet().forEach(e -> dumpEntry(p, indent, e));
}

protected void dumpEntry (PrintStream p, String indent, Map.Entry<Object,Object> entry) {
Expand All @@ -298,7 +296,11 @@ protected void dumpEntry (PrintStream p, String indent, Map.Entry<Object,Object>
Object value = entry.getValue();
if (value instanceof Loggeable) {
p.println("");
((Loggeable) value).dump(p, indent + " ");
try {
((Loggeable) value).dump(p, indent + " ");
} catch (Exception ex) {
ex.printStackTrace(p);
Copy link
Contributor

@alcarraz alcarraz Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be nice to also print an explanation of what happened, so it is clearer at a first look to the std err, that it was an exception while dumping before having to pay attention to the stack trace.

This could even be a new log event to be sent to the default logger, with the message and the exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not printing to stderr, it goes to the PrintStream, along with the rest of the message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last time I checked ex.printStackTrace, printed to std err.

Prints this throwable and its backtrace to the standard error stream. This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. The first line of output contains the result of the toString() method for this object. Remaining lines represent data previously recorded by the method fillInStackTrace(). The format of this information depends on the implementation, but the following example may be regarded as typical:

Copy link
Contributor

@alcarraz alcarraz Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oups, sorry, I didn't see the pint stream parameter 🤦🏼‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my tests, it will appear like

      ACCOUNT: org.hibernate.LazyInitializationException: could not initialize proxy [org.jpos.gl.FinalAccount#1980] - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:322)
    ... (more stack trace) ...

and then continue with the rest of the ctx dump as usual, closing every XML tag correctly

(at least for the toString() case... not sure the Loggeable one)

}
p.print(indent);
} else if (value instanceof Element) {
p.println("");
Expand Down Expand Up @@ -329,10 +331,15 @@ else if (value instanceof LogEvent) {
((LogEvent) value).dump(p, indent);
p.print(indent);
} else if (value != null) {
LogUtil.dump(p, indent, value.toString());
try {
LogUtil.dump(p, indent, value.toString());
} catch (Exception ex) {
ex.printStackTrace(p);
}
}
p.println();
}

/**
* return a LogEvent used to store trace information
* about this transaction.
Expand Down Expand Up @@ -402,7 +409,7 @@ public PausedTransaction getPausedTransaction() {
public PausedTransaction getPausedTransaction(long timeout) {
return get (PAUSED_TRANSACTION.toString(), timeout);
}

public void setTimeout (long timeout) {
this.timeout = timeout;
}
Expand Down
Loading