Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions core/src/main/java/hudson/slaves/OfflineCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ public String getShortDescription() {
return cause.toString();
}

/**
* Returns the cause of the channel termination if it's an ExportedBean,
* null otherwise. This allows the API to export the cause details when
* the exception has the @ExportedBean annotation.
*
* @return the cause if it's exportable, null otherwise
* @since 2.529
*/
@Exported
public Exception getCause() {
if (cause != null && cause.getClass().isAnnotationPresent(ExportedBean.class)) {
return cause;
}
return null;
}

@Override public String toString() {
return Messages.OfflineCause_connection_was_broken_simple();
}
Expand Down
51 changes: 51 additions & 0 deletions core/src/test/java/hudson/slaves/OfflineCauseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;

import org.junit.jupiter.api.Test;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

class OfflineCauseTest {

Expand All @@ -15,4 +19,51 @@ void testChannelTermination_NoStacktrace() {
assertThat(cause.toString(), not(containsString(exceptionMessage)));
}

@ExportedBean
static class ExportableException extends Exception {
private final String testMessage;

ExportableException(String message) {
super(message);
this.testMessage = message;
}

@Exported
public String getTestMessage() {
return testMessage;
}
}

static class NonExportableException extends Exception {
NonExportableException(String message) {
super(message);
}
}

@Test
void testChannelTermination_ExportsCauseWhenExportedBean() {
ExportableException exportableException = new ExportableException("test message");
OfflineCause.ChannelTermination termination = new OfflineCause.ChannelTermination(exportableException);

// The getCause() method should return the exception when it's an ExportedBean
assertThat(termination.getCause(), sameInstance(exportableException));
}

@Test
void testChannelTermination_DoesNotExportNonExportableException() {
NonExportableException nonExportableException = new NonExportableException("test message");
OfflineCause.ChannelTermination termination = new OfflineCause.ChannelTermination(nonExportableException);

// The getCause() method should return null when the exception is not an ExportedBean
assertThat(termination.getCause(), nullValue());
}

@Test
void testChannelTermination_HandlesNullCause() {
OfflineCause.ChannelTermination termination = new OfflineCause.ChannelTermination(null);

// The getCause() method should handle null cause
assertThat(termination.getCause(), nullValue());
}

}
Loading