{"record":{"id":"9239bd9fc3fecd2d","repo":"jenkinsci/jenkins","slug":"no-ancestor-of-type-in-the-request","errorCode":null,"errorMessage":"No ancestor of type {} in the request","messagePattern":"No ancestor of type (.+?) in the request","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/hudson/Util.java","lineNumber":1910,"sourceCode":"     * @return positive number of days between the given date and now\n     * @see #daysBetween(Date, Date)\n     */\n    @Restricted(NoExternalUse.class)\n    public static long daysElapsedSince(@NonNull Date date) {\n        return Math.max(0, daysBetween(date, new Date()));\n    }\n\n    /**\n     * Find the specific ancestor, or throw an exception.\n     * Useful for an ancestor we know is inside the URL to ease readability\n     *\n     * @since 2.475\n     */\n    @Restricted(NoExternalUse.class)\n    public static @NonNull <T> T getNearestAncestorOfTypeOrThrow(@NonNull StaplerRequest2 request, @NonNull Class<T> clazz) {\n        T t = request.findAncestorObject(clazz);\n        if (t == null) {\n            throw new IllegalArgumentException(\"No ancestor of type \" + clazz.getName() + \" in the request\");\n        }\n        return t;\n    }\n\n    /**\n     * @deprecated use {@link #getNearestAncestorOfTypeOrThrow(StaplerRequest2, Class)}\n     */\n    @Deprecated\n    @Restricted(NoExternalUse.class)\n    public static @NonNull <T> T getNearestAncestorOfTypeOrThrow(@NonNull StaplerRequest request, @NonNull Class<T> clazz) {\n        return getNearestAncestorOfTypeOrThrow(StaplerRequest.toStaplerRequest2(request), clazz);\n    }\n\n    @Restricted(NoExternalUse.class)\n    public static void printRedirect(String contextPath, String redirectUrl, String message, PrintWriter out) {\n        out.printf(\n                \"<html><head>\" +\n                \"<meta http-equiv='refresh' content='1;url=%1$s'/>\" +","sourceCodeStart":1892,"sourceCodeEnd":1928,"githubUrl":"https://github.com/jenkinsci/jenkins/blob/2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc/core/src/main/java/hudson/Util.java#L1892-L1928","documentation":"Thrown by Util.getNearestAncestorOfTypeOrThrow when the StaplerRequest's findAncestorObject(clazz) returns null — meaning the current request URL path does not traverse through any object of the requested type. This utility enforces that a required ancestor object (e.g., a Job, View, or Jenkins instance) is present in the URL hierarchy.","triggerScenarios":"The Stapler request URL does not contain a path segment that maps to an object of type T. For example, calling getNearestAncestorOfTypeOrThrow(request, Job.class) from a URL like /jenkins/configure that does not traverse a Job object.","commonSituations":"A Stapler-bound URL handler or jelly view calls this utility expecting a parent object that is only present on specific URL paths (e.g., /job/<name>/...); the endpoint is accessed from a different URL context where the ancestor is absent; a plugin routes to a view from a non-standard path that skips the expected parent.","solutions":["Ensure the URL path that triggers this code includes the expected ancestor object (e.g., access the view via /job/<jobname>/... rather than a top-level path).","If the ancestor may legitimately be absent, use request.findAncestorObject(clazz) directly and handle the null case instead of the throwing variant.","Verify Stapler URL bindings and StaplerRequest routing to ensure the expected object is in the ancestor chain."],"exampleFix":"// before\nJob job = Util.getNearestAncestorOfTypeOrThrow(request, Job.class);\n\n// after\nJob job = request.findAncestorObject(Job.class);\nif (job == null) {\n    // handle missing ancestor gracefully (redirect, error page, etc.)\n    return; \n}","handlingStrategy":"type-guard","validationCode":"// Check ancestor presence before calling\nT ancestor = request.findAncestorObject(clazz);\nif (ancestor == null) {\n    // handle: redirect, show error, or use default\n    throw new CmdLineException(\"This action requires a \" + clazz.getSimpleName() + \" in the URL path.\");\n}","typeGuard":"public static <T> boolean hasAncestorOfType(StaplerRequest2 req, Class<T> clazz) {\n    return req.findAncestorObject(clazz) != null;\n}","tryCatchPattern":"try {\n    T ancestor = Util.getNearestAncestorOfTypeOrThrow(request, clazz);\n} catch (IllegalArgumentException e) {\n    // No ancestor of type T in the URL — return 404 or redirect\n    res.sendError(HttpServletResponse.SC_NOT_FOUND);\n    return;\n}","preventionTips":["Ensure Stapler URL bindings route through the expected ancestor object path.","Use the non-throwing variant (findAncestorObject) when absence is a valid condition.","Test endpoints from multiple URL entry points to verify ancestor presence."],"tags":["stapler","request","url-binding","validation"],"backgroundTag":null,"analyzedSha":"2e228ff40b14dbc8b14ffbc6edf0e4383cf744fc","analyzedAt":"2026-08-14T07:07:15.274Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}