Skip to content
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

Define HttpResponses.wrap and use from RequestImpl.invokeConstructor #571

Merged
merged 2 commits into from
Jul 26, 2024
Merged
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
23 changes: 23 additions & 0 deletions core/src/main/java/org/kohsuke/stapler/HttpResponses.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object nod
};
}

/**
* A runtime exception which honors a defined response.
*/
public static HttpResponseException wrap(HttpResponse delegate) {
if (delegate instanceof Throwable) {
return new HttpResponseException(((Throwable) delegate).getMessage(), (Throwable) delegate) {
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
throws IOException, ServletException {
delegate.generateResponse(req, rsp, node);
}
};
} else {
return new HttpResponseException() {
@Override
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
throws IOException, ServletException {
delegate.generateResponse(req, rsp, node);
}
};
}
}

public static HttpResponseException redirectViaContextPath(String relative) {
return redirectViaContextPath(HttpServletResponse.SC_MOVED_TEMPORARILY, relative);
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@
if (x instanceof RuntimeException) {
throw (RuntimeException) x;
}
// TODO apply similar logic to other catchers of InvocationTargetException as needed

Check warning on line 674 in core/src/main/java/org/kohsuke/stapler/RequestImpl.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: apply similar logic to other catchers of InvocationTargetException as needed
if (x instanceof HttpResponse) {
throw HttpResponses.wrap((HttpResponse) x);
}
throw new IllegalArgumentException(x);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Failed to invoke " + c + " with " + Arrays.asList(args), e);
Expand Down