Skip to content

Commit

Permalink
Merge pull request #571 from jglick/HttpResponses.wrap
Browse files Browse the repository at this point in the history
Define `HttpResponses.wrap` and use from `RequestImpl.invokeConstructor`
  • Loading branch information
jglick authored Jul 26, 2024
2 parents 68467ae + 4efde02 commit 73465f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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 @@ private <T> T invokeConstructor(Constructor<T> c, Object[] args) {
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

0 comments on commit 73465f3

Please sign in to comment.