-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 6828 payara issue reproducer by adding 2 endpoints throwing EJ…
…BException wrapped and non-wrapped ValidationExceptions that should be captured by the provided custom EJBExceptionMapper and ValidationExceptionMapper
- Loading branch information
Gael Abadin
committed
Jul 19, 2024
1 parent
7bcb0c6
commit 70187e6
Showing
11 changed files
with
199 additions
and
9 deletions.
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
db.port=${db.port} | ||
payara.port=${payara.port} | ||
payara.host=localhost | ||
payara.teardownDelay=1000 |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: '3.3' | ||
services: | ||
db: | ||
image: postgres:latest | ||
|
55 changes: 55 additions & 0 deletions
55
hello-payara-world-war/src/main/java/com/example/payara/hello/EJBExceptionMapper.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,55 @@ | ||
package com.example.payara.hello; | ||
|
||
import jakarta.ejb.EJBException; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
import jakarta.ws.rs.ext.Providers; | ||
|
||
/** | ||
* Maps {@link EJBException} thrown by e.g. EJB services and tries to unwrap and rethrow the exception. | ||
*/ | ||
@Provider | ||
public class EJBExceptionMapper implements ExceptionMapper<EJBException> { | ||
|
||
@Context | ||
private Providers providers; | ||
|
||
@Override | ||
public Response toResponse(final EJBException exception) { | ||
|
||
Throwable handledException = exception; | ||
|
||
try { | ||
unwrapEJBException(exception); | ||
} catch (Throwable e) { | ||
// Exception is unwrapped into something not EJBException related | ||
// Try to get the mapper for it | ||
handledException = e; | ||
} | ||
|
||
ExceptionMapper<Throwable> mapper = providers.getExceptionMapper((Class<Throwable>) handledException.getClass()); | ||
return mapper.toResponse(handledException); | ||
} | ||
|
||
/** | ||
* Throws the first nested exception which isn't an {@link EJBException}. | ||
* | ||
* @param wrappingException the EJBException | ||
*/ | ||
public static void unwrapEJBException(final EJBException wrappingException) throws Throwable { | ||
Throwable pE = null; | ||
Throwable cE = wrappingException; | ||
while (cE != null && cE.getCause() != pE) { | ||
if (!(cE instanceof EJBException)) { | ||
throw cE; | ||
} | ||
pE = cE; | ||
cE = cE.getCause(); | ||
} | ||
if (cE != null) { | ||
throw cE; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
hello-payara-world-war/src/main/java/com/example/payara/hello/ErrorResponse.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,33 @@ | ||
package com.example.payara.hello; | ||
|
||
/** | ||
* The generic error response. | ||
*/ | ||
public class ErrorResponse { | ||
|
||
// Status code of the HTTP response | ||
private Integer code; | ||
// A human-readable description of the error | ||
private String message; | ||
|
||
public ErrorResponse() { | ||
// NOOP | ||
} | ||
|
||
public Integer getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(Integer code) { | ||
this.code = code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
hello-payara-world-war/src/main/java/com/example/payara/hello/ValidationExceptionMapper.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,21 @@ | ||
package com.example.payara.hello; | ||
|
||
import jakarta.validation.ValidationException; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> { | ||
|
||
@Override | ||
public Response toResponse(final ValidationException exception) { | ||
var errorResponse = new ErrorResponse(); | ||
errorResponse.setCode(Response.Status.BAD_REQUEST.getStatusCode()); | ||
return Response.status(Response.Status.BAD_REQUEST) | ||
.entity(errorResponse) | ||
.type(MediaType.APPLICATION_JSON_TYPE).build(); | ||
} | ||
|
||
} |
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