From e859e9cd9c87a34153bc02c02cdfc174daa5c0e3 Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Fri, 5 Jan 2024 20:40:12 +0000 Subject: [PATCH] Allow plugins to override boot failure view (#8442) --- core/src/main/java/hudson/WebAppMain.java | 26 ++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/WebAppMain.java b/core/src/main/java/hudson/WebAppMain.java index 99603ac1e4..9f328328c7 100644 --- a/core/src/main/java/hudson/WebAppMain.java +++ b/core/src/main/java/hudson/WebAppMain.java @@ -48,6 +48,7 @@ import hudson.util.RingBufferLogHandler; import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.Charset; @@ -261,13 +262,36 @@ public class WebAppMain implements ServletContextListener { new HudsonFailedToLoad(e).publish(context, _home); throw e; } catch (Exception e) { - new HudsonFailedToLoad(e).publish(context, _home); + // Allow plugins to override error page on boot with custom BootFailure subclass thrown + Throwable error = unwrapException(e); + if (error instanceof InvocationTargetException) { + Throwable targetException = ((InvocationTargetException) error).getTargetException(); + + if (targetException instanceof BootFailure) { + ((BootFailure) targetException).publish(context, _home); + } else { + new HudsonFailedToLoad(e).publish(context, _home); + } + } else { + new HudsonFailedToLoad(e).publish(context, _home); + } } finally { Jenkins instance = Jenkins.getInstanceOrNull(); if (!success && instance != null) instance.cleanUp(); } } + + private Throwable unwrapException(Exception e) { + Throwable error = e; + while (error.getCause() != null) { + if (error.getCause() instanceof InvocationTargetException) { + return error.getCause(); + } + error = error.getCause(); + } + return error; + } }; initThread.start(); } catch (BootFailure e) {