-
Notifications
You must be signed in to change notification settings - Fork 129
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
[JENKINS-28119] ErrorInfoStep #2
Draft
jglick
wants to merge
3
commits into
jenkinsci:master
Choose a base branch
from
jglick:JENKINS-28119-errorInfo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
src/main/java/org/jenkinsci/plugins/workflow/steps/ErrorInfoStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2016 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.steps; | ||
|
||
import com.google.inject.Inject; | ||
import hudson.AbortException; | ||
import hudson.Extension; | ||
import hudson.Functions; | ||
import hudson.model.Result; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.annotation.CheckForNull; | ||
import javax.annotation.Nonnull; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; | ||
import org.jenkinsci.plugins.workflow.actions.ErrorAction; | ||
import org.jenkinsci.plugins.workflow.actions.LogAction; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecution; | ||
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner; | ||
import org.jenkinsci.plugins.workflow.graph.BlockEndNode; | ||
import org.jenkinsci.plugins.workflow.graph.FlowGraphWalker; | ||
import org.jenkinsci.plugins.workflow.graph.FlowNode; | ||
import org.jenkinsci.plugins.workflow.graph.FlowNodeSerialWalker; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
/** | ||
* Step to supply contextual information about an error that has been caught. | ||
*/ | ||
public class ErrorInfoStep extends AbstractStepImpl { | ||
|
||
public final Throwable error; | ||
|
||
@DataBoundConstructor public ErrorInfoStep(Throwable error) { | ||
this.error = error; | ||
} | ||
|
||
public static class Execution extends AbstractSynchronousStepExecution<ErrorInfo> { | ||
|
||
@Inject private ErrorInfoStep step; | ||
@StepContextParameter private FlowExecution execution; | ||
|
||
@Override protected ErrorInfo run() throws Exception { | ||
return new ErrorInfo(step.error, execution); | ||
} | ||
|
||
} | ||
|
||
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl { | ||
|
||
public DescriptorImpl() { | ||
super(Execution.class); | ||
} | ||
|
||
@Override public String getFunctionName() { | ||
return "errorInfo"; | ||
} | ||
|
||
@Override public String getDisplayName() { | ||
return "Calculate information about an error"; | ||
} | ||
|
||
// TODO blank config.jelly | ||
|
||
} | ||
|
||
public static class ErrorInfo implements Serializable { | ||
|
||
private static final long serialVersionUID = 1; | ||
private final Throwable error; | ||
private transient FlowExecution execution; | ||
private final FlowExecutionOwner executionOwner; | ||
|
||
ErrorInfo(Throwable error, FlowExecution execution) { | ||
this.error = error; | ||
this.execution = execution; | ||
executionOwner = execution.getOwner(); | ||
} | ||
|
||
private FlowExecution getExecution() throws IOException { | ||
if (execution == null) { | ||
execution = executionOwner.get(); | ||
} | ||
return execution; | ||
} | ||
|
||
/** | ||
* Finds a node which threw this exception or one of its causes. | ||
* Note that {@link Throwable#equals} is just pointer equality, | ||
* which we cannot use since we may be loading deserialized exceptions, | ||
* so we compare by stack trace instead. | ||
*/ | ||
private @CheckForNull FlowNode getNode() throws IOException { | ||
Set<String> stackTraces = new HashSet<>(); | ||
for (Throwable t = error; t != null; t = t.getCause()) { | ||
stackTraces.add(Functions.printThrowable(t)); | ||
} | ||
for (FlowNode n : new FlowGraphWalker(getExecution())) { | ||
if (n instanceof BlockEndNode) { | ||
continue; // look for the thing it is enclosing | ||
} | ||
ErrorAction a = n.getAction(ErrorAction.class); | ||
if (a != null) { | ||
if (stackTraces.contains(Functions.printThrowable(a.getError()))) { | ||
return n; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Whitelisted | ||
public @Nonnull Throwable getError() { | ||
return error; | ||
} | ||
|
||
/** | ||
* Gets the stack trace of the error, or just the message in the case of {@link AbortException}. | ||
*/ | ||
@Whitelisted | ||
public @Nonnull String getStackTrace() { | ||
if (error instanceof AbortException) { | ||
return error.getMessage(); | ||
} else { | ||
return Functions.printThrowable(error); | ||
} | ||
} | ||
|
||
/** | ||
* Gets the {@link Result} of the build if the error were uncaught. | ||
* @return typically {@link Result#FAILURE} but {@link FlowInterruptedException} may override | ||
*/ | ||
@Whitelisted | ||
public @Nonnull String getResult() { | ||
Result r; | ||
if (error instanceof FlowInterruptedException) { | ||
r = ((FlowInterruptedException) error).getResult(); | ||
} else { | ||
r = Result.FAILURE; | ||
} | ||
return r.toString(); | ||
} | ||
|
||
/** | ||
* Looks for the URL of the {@link LogAction} last printed before the node which broke. | ||
*/ | ||
@Whitelisted | ||
public @CheckForNull String getLogURL() throws IOException { | ||
FlowNode n = getNode(); | ||
if (n != null) { | ||
for (FlowNode n2 : new FlowNodeSerialWalker(n)) { | ||
LogAction a = n2.getAction(LogAction.class); | ||
if (a != null) { | ||
String u = Jenkins.getInstance().getRootUrl(); | ||
if (u == null) { | ||
u = "http://jenkins/"; // placeholder | ||
} | ||
return u + n2.getUrl() + a.getUrlName(); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// TODO tail of log | ||
// TODO label | ||
|
||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
src/test/java/org/jenkinsci/plugins/workflow/steps/ErrorInfoStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2016 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.steps; | ||
|
||
import hudson.AbortException; | ||
import hudson.model.Result; | ||
import java.net.URL; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep; | ||
import static org.junit.Assert.*; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.Rule; | ||
import org.junit.runners.model.Statement; | ||
import org.jvnet.hudson.test.BuildWatcher; | ||
import org.jvnet.hudson.test.Issue; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.RestartableJenkinsRule; | ||
|
||
public class ErrorInfoStepTest { | ||
|
||
@ClassRule public static BuildWatcher buildWatcher = new BuildWatcher(); | ||
@Rule public RestartableJenkinsRule s = new RestartableJenkinsRule(); | ||
|
||
@Issue("JENKINS-28119") | ||
@Test public void smokes() { | ||
s.addStep(new Statement() { | ||
@Override public void evaluate() throws Throwable { | ||
WorkflowJob p = s.j.jenkins.createProject(WorkflowJob.class, "p"); | ||
p.setDefinition(new CpsFlowDefinition( | ||
"try {\n" + | ||
" parallel fine: {\n" + | ||
" semaphore 'fine'\n" + | ||
" }, broken: {\n" + | ||
" echo 'erroneous step'\n" + | ||
" semaphore 'breaking'\n" + | ||
" }\n" + | ||
"} catch (e) {\n" + | ||
" def info = errorInfo(e)\n" + | ||
" semaphore 'caught'\n" + | ||
" currentBuild.result = info.result\n" + | ||
" echo \"caught an instance of ${info.error.getClass()}\"\n" + | ||
" echo info.stackTrace\n" + | ||
" echo \"browse to: ${info.logURL}\"\n" + | ||
"}", true)); | ||
WorkflowRun b = p.scheduleBuild2(0).waitForStart(); | ||
SemaphoreStep.waitForStart("fine/1", b); | ||
SemaphoreStep.failure("breaking/1", new AbortException("oops")); | ||
SemaphoreStep.success("fine/1", null); | ||
SemaphoreStep.waitForStart("caught/1", null); | ||
} | ||
}); | ||
s.addStep(new Statement() { | ||
@Override public void evaluate() throws Throwable { | ||
SemaphoreStep.success("caught/1", null); | ||
WorkflowJob p = s.j.jenkins.getItemByFullName("p", WorkflowJob.class); | ||
WorkflowRun b = p.getBuildByNumber(1); | ||
s.j.assertBuildStatus(Result.FAILURE, s.j.waitForCompletion(b)); | ||
s.j.waitForMessage("End of Pipeline", b); // TODO why does it sometimes cut off at "Resuming build"? probably because WorkflowRun.finish sets isBuilding() → false before flushing the log | ||
s.j.assertLogContains("caught an instance of class hudson.AbortException", b); | ||
s.j.assertLogContains("oops", b); | ||
s.j.assertLogNotContains("\tat ", b); | ||
String log = JenkinsRule.getLog(b); | ||
Matcher matcher = Pattern.compile("^browse to: (http.+)$", Pattern.MULTILINE).matcher(log); | ||
assertTrue(log, matcher.find()); | ||
String text = s.j.createWebClient().getPage(new URL(matcher.group(1))).getWebResponse().getContentAsString(); | ||
assertTrue(text, text.contains("erroneous step")); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Downstream of jenkinsci/workflow-api-plugin#1.