-
Notifications
You must be signed in to change notification settings - Fork 727
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
Nicer error pages #2352
Merged
Merged
Nicer error pages #2352
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
690edb0
Nicer error pages
timja 7277971
Start tests
timja d332087
Spotless
timja 3c50cf6
Handle a few more cases better
timja 1f120b9
All working
timja f76cdf1
Delete test that I just can't figure out a way to do
timja d6a5a75
Adapt tests
timja 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
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 |
---|---|---|
|
@@ -68,6 +68,7 @@ | |
import java.util.stream.Stream; | ||
import javax.inject.Inject; | ||
import javax.servlet.ServletContext; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletResponse; | ||
import jenkins.model.GlobalConfiguration; | ||
import jenkins.model.Jenkins; | ||
|
@@ -169,10 +170,42 @@ public void doReload(StaplerRequest request, StaplerResponse response) throws Ex | |
response.sendError(HttpServletResponse.SC_FORBIDDEN); | ||
return; | ||
} | ||
configure(); | ||
|
||
try { | ||
configure(); | ||
} catch (ConfiguratorException e) { | ||
LOGGER.log(Level.SEVERE, "Failed to reload configuration", e); | ||
|
||
Throwable throwableCause = e.getCause(); | ||
if (throwableCause instanceof ConfiguratorException) { | ||
ConfiguratorException cause = (ConfiguratorException) throwableCause; | ||
|
||
handleExceptionOnReloading(request, response, cause); | ||
} else { | ||
handleExceptionOnReloading(request, response, e); | ||
} | ||
return; | ||
} | ||
response.sendRedirect(""); | ||
} | ||
|
||
@Restricted(NoExternalUse.class) | ||
public static void handleExceptionOnReloading( | ||
StaplerRequest request, StaplerResponse response, ConfiguratorException cause) | ||
throws ServletException, IOException { | ||
Configurator<?> configurator = cause.getConfigurator(); | ||
request.setAttribute("errorMessage", cause.getErrorMessage()); | ||
if (configurator != null) { | ||
request.setAttribute("target", configurator.getName()); | ||
} else if (cause instanceof UnknownConfiguratorException) { | ||
List<String> configuratorNames = ((UnknownConfiguratorException) cause).getConfiguratorNames(); | ||
request.setAttribute("target", String.join(", ", configuratorNames)); | ||
} | ||
request.setAttribute("invalidAttribute", cause.getInvalidAttribute()); | ||
request.setAttribute("validAttributes", cause.getValidAttributes()); | ||
request.getView(ConfigurationAsCode.class, "error.jelly").forward(request, response); | ||
} | ||
|
||
@RequirePOST | ||
@Restricted(NoExternalUse.class) | ||
public void doReplace(StaplerRequest request, StaplerResponse response) throws Exception { | ||
|
@@ -303,7 +336,11 @@ private List<YamlSource> getConfigFromSources(List<String> newSources) throws Co | |
@Initializer(after = InitMilestone.SYSTEM_CONFIG_LOADED, before = InitMilestone.SYSTEM_CONFIG_ADAPTED) | ||
public static void init() throws Exception { | ||
detectVaultPluginMissing(); | ||
get().configure(); | ||
try { | ||
get().configure(); | ||
} catch (ConfiguratorException e) { | ||
throw new ConfigurationAsCodeBootFailure(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This requires jenkinsci/jenkins#8442 to work properly although it won't do any harm if not present |
||
} | ||
} | ||
|
||
/** | ||
|
@@ -727,18 +764,9 @@ private static void invokeWith(Mapping entries, ConfiguratorOperation function) | |
if (!entry.getKey().equalsIgnoreCase(configurator.getName())) { | ||
continue; | ||
} | ||
try { | ||
function.apply(configurator, entry.getValue()); | ||
it.remove(); | ||
break; | ||
} catch (ConfiguratorException e) { | ||
throw new ConfiguratorException( | ||
configurator, | ||
format( | ||
"error configuring '%s' with %s configurator", | ||
entry.getKey(), configurator.getClass()), | ||
e); | ||
} | ||
function.apply(configurator, entry.getValue()); | ||
it.remove(); | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -752,8 +780,7 @@ private static void invokeWith(Mapping entries, ConfiguratorOperation function) | |
}); | ||
|
||
if (!unknownKeys.isEmpty()) { | ||
throw new ConfiguratorException( | ||
format("No configurator for the following root elements %s", String.join(", ", unknownKeys))); | ||
throw new UnknownConfiguratorException(unknownKeys, "No configurator for the following root elements:"); | ||
} | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCodeBootFailure.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,19 @@ | ||
package io.jenkins.plugins.casc; | ||
|
||
import hudson.util.BootFailure; | ||
import java.io.IOException; | ||
import javax.servlet.ServletException; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
public class ConfigurationAsCodeBootFailure extends BootFailure { | ||
|
||
public ConfigurationAsCodeBootFailure(ConfiguratorException cause) { | ||
super(cause); | ||
} | ||
|
||
public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { | ||
rsp.setStatus(503); | ||
ConfigurationAsCode.handleExceptionOnReloading(req, rsp, (ConfiguratorException) getCause()); | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
plugin/src/main/java/io/jenkins/plugins/casc/UnknownAttributesException.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,23 @@ | ||
package io.jenkins.plugins.casc; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import java.util.List; | ||
|
||
public class UnknownAttributesException extends ConfiguratorException { | ||
|
||
private final String errorMessage; | ||
|
||
public UnknownAttributesException( | ||
@CheckForNull Configurator configurator, | ||
String simpleMessage, | ||
@CheckForNull String message, | ||
String invalidAttribute, | ||
List<String> validAttributes) { | ||
super(configurator, message, invalidAttribute, validAttributes, null); | ||
this.errorMessage = simpleMessage; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
plugin/src/main/java/io/jenkins/plugins/casc/UnknownConfiguratorException.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,23 @@ | ||
package io.jenkins.plugins.casc; | ||
|
||
import java.util.List; | ||
|
||
public class UnknownConfiguratorException extends ConfiguratorException { | ||
|
||
private final List<String> configuratorNames; | ||
private final String errorMessage; | ||
|
||
public UnknownConfiguratorException(List<String> configuratorNames, String errorMessage) { | ||
super(errorMessage + String.join(", ", configuratorNames)); | ||
this.errorMessage = errorMessage; | ||
this.configuratorNames = configuratorNames; | ||
} | ||
|
||
public String getErrorMessage() { | ||
return errorMessage; | ||
} | ||
|
||
public List<String> getConfiguratorNames() { | ||
return configuratorNames; | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
plugin/src/main/resources/io/jenkins/plugins/casc/ConfigurationAsCode/error.jelly
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,24 @@ | ||
<?jelly escape-by-default='true'?> | ||
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout"> | ||
<l:layout type="one-column" title="${%Error loading configuration}"> | ||
<l:main-panel> | ||
<l:app-bar title="${%Error loading configuration}" /> | ||
|
||
<p>${errorMessage}<pre><code>${target}</code></pre></p> | ||
|
||
<j:if test="${invalidAttribute != null}"> | ||
<p>Attribute was: <pre><code>${invalidAttribute}</code></pre></p> | ||
</j:if> | ||
|
||
<j:if test="${!empty(validAttributes)}"> | ||
<p>Valid attributes are:</p> | ||
|
||
<ul> | ||
<j:forEach var="attribute" items="${validAttributes}"> | ||
<li>${attribute}</li> | ||
</j:forEach> | ||
</ul> | ||
</j:if> | ||
</l:main-panel> | ||
</l:layout> | ||
</j:jelly> |
Oops, something went wrong.
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.
FYI I am getting
in CloudBees CI after a CasC error during startup. Not sure yet if this is reproducible.