-
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.
WAExHelper to build WebApplicationException
- Loading branch information
Showing
11 changed files
with
312 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>emp-exception-mapper</artifactId> | ||
|
||
<parent> | ||
<groupId>org.fugerit.java</groupId> | ||
<artifactId>fj-service-helper-bom</artifactId> | ||
<version>1.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<name>Eclipse MicroProfile Exception Mapper</name> | ||
<description>Collection of MicroProfile ExceptionMappers</description> | ||
|
||
<licenses> | ||
<license> | ||
<name>Apache License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
|
||
<build> | ||
|
||
<plugins> | ||
|
||
<plugin> | ||
<groupId>org.fugerit.java</groupId> | ||
<artifactId>native-helper-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>generate-native-configuration</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>generate</goal> | ||
</goals> | ||
<configuration> | ||
<nativeHelperConfigPath>src/main/config/native-helper-config.yaml</nativeHelperConfigPath> | ||
<reflectConfigJsonOutputPath>${project.build.outputDirectory}/META-INF/native-image/reflect-config.json</reflectConfigJsonOutputPath> | ||
<warnOnError>true</warnOnError> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
</plugins> | ||
|
||
</build> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.fugerit.java</groupId> | ||
<artifactId>emp-service-model</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>jakarta.ws.rs</groupId> | ||
<artifactId>jakarta.ws.rs-api</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-rest-client-jackson</artifactId> | ||
<version>${quarkus.platform.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<organization> | ||
<url>https://www.fugerit.org</url> | ||
<name>Fugerit</name> | ||
</organization> | ||
|
||
<url>https://www.fugerit.org/perm/venus/</url> | ||
|
||
</project> |
27 changes: 27 additions & 0 deletions
27
emp-exception-mapper/src/main/java/org/fugerit/java/emp/em/GenericExceptionMapper.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,27 @@ | ||
package org.fugerit.java.emp.em; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.fugerit.java.emp.sm.service.ServiceResponse; | ||
import org.fugerit.java.emp.sm.service.ServiceResponseHelper; | ||
|
||
@Slf4j | ||
@Provider | ||
public class GenericExceptionMapper implements ExceptionMapper<WebApplicationException> { | ||
@Override | ||
public Response toResponse(WebApplicationException ex) { | ||
if ( ex.getResponse().getEntity() != null || ex.getMessage() == null ) { | ||
return ex.getResponse(); | ||
} else { | ||
Response.Status status = Response.Status.fromStatusCode( ex.getResponse().getStatus() ); | ||
return Response.status( status ).entity( | ||
new ServiceResponse().addAllBySeverity( ServiceResponseHelper.newMessageByStatus( status, ex.getMessage() ) ) | ||
).build(); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...ception-mapper/src/test/java/test/org/fugerit/java/emp/em/TestGenericExceptionMapper.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,27 @@ | ||
package test.org.fugerit.java.emp.em; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.fugerit.java.emp.em.GenericExceptionMapper; | ||
import org.fugerit.java.emp.ex.WAExHelper; | ||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.fugerit.java.emp.sm.service.ServiceResponse; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@Slf4j | ||
class TestGenericExceptionMapper { | ||
|
||
private static final String TEST_MESSAGE = "test error"; | ||
|
||
@Test | ||
void test() { | ||
GenericExceptionMapper gem = new GenericExceptionMapper(); | ||
Response r1 = gem.toResponse( WAExHelper.newEx( TEST_MESSAGE+"1" ) ); | ||
Assertions.assertEquals( TEST_MESSAGE+"1", ((ServiceResponse)r1.getEntity()).getErrors().get( 0 ).getText() ); | ||
Response r2 = gem.toResponse( new WebApplicationException( TEST_MESSAGE+"2", (Response) null ) ); | ||
Assertions.assertEquals( TEST_MESSAGE+"2", ((ServiceResponse)r2.getEntity()).getErrors().get( 0 ).getText() ); | ||
} | ||
|
||
} |
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
41 changes: 41 additions & 0 deletions
41
emp-service-model/src/main/java/org/fugerit/java/emp/ex/WAExHelper.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,41 @@ | ||
package org.fugerit.java.emp.ex; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.fugerit.java.emp.sm.service.ServiceResponse; | ||
import org.fugerit.java.emp.sm.service.ServiceResponseHelper; | ||
|
||
public class WAExHelper { | ||
|
||
private WAExHelper() {} | ||
|
||
public static WebApplicationException newEx( String message) { | ||
return newEx( Response.Status.INTERNAL_SERVER_ERROR, message ); | ||
} | ||
|
||
public static WebApplicationException newEx( Response.Status status, String message ) { | ||
return newEx( null, status, message ); | ||
} | ||
|
||
public static WebApplicationException newEx( Throwable cause, Response.Status status, String message ) { | ||
return newEx( cause, status, ServiceResponseHelper.newMessageByStatus( status, message ) ); | ||
} | ||
|
||
public static WebApplicationException newEx( Response.Status status, ServiceMessage sm ) { | ||
return newEx( null, status, sm ); | ||
} | ||
|
||
public static WebApplicationException newEx( Throwable cause, Response.Status status, ServiceMessage sm ) { | ||
return newEx( cause, status, new ServiceResponse().addAllBySeverity( sm ) ); | ||
} | ||
|
||
public static WebApplicationException newEx( Response.Status status, ServiceResponse sr ) { | ||
return newEx( null, status, sr ); | ||
} | ||
|
||
public static WebApplicationException newEx( Throwable cause, Response.Status status, ServiceResponse sr ) { | ||
return new WebApplicationException( cause , Response.status( status ).entity( sr ).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
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
29 changes: 29 additions & 0 deletions
29
emp-service-model/src/test/java/test/org/fugerit/java/emp/ex/TestWAExHelper.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,29 @@ | ||
package test.org.fugerit.java.emp.ex; | ||
|
||
import jakarta.ws.rs.WebApplicationException; | ||
import jakarta.ws.rs.core.Response; | ||
import org.fugerit.java.emp.ex.WAExHelper; | ||
import org.fugerit.java.emp.sm.service.ServiceMessage; | ||
import org.fugerit.java.emp.sm.service.ServiceResponse; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TestWAExHelper { | ||
|
||
private static final String TEST_MESSAGE = "My Message"; | ||
|
||
private ServiceResponse getResponse(WebApplicationException ex) { | ||
return ((ServiceResponse)ex.getResponse().getEntity()); | ||
} | ||
|
||
private String getFirstError(WebApplicationException ex) { | ||
return this.getResponse( ex ).getErrors().get( 0 ).getText(); | ||
} | ||
|
||
@Test | ||
void test() { | ||
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx( TEST_MESSAGE ) ) ); | ||
Assertions.assertEquals( TEST_MESSAGE, this.getFirstError( WAExHelper.newEx(Response.Status.INTERNAL_SERVER_ERROR, TEST_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
Oops, something went wrong.