Skip to content
Snippets Groups Projects
Commit 4a28d5fc authored by Yash Dholakia's avatar Yash Dholakia
Browse files

Cherry pick error handling and core-common upgrade

parent 6789f9be
No related branches found
No related tags found
2 merge requests!301trusted-delta-changes-from-m16,!247Error Handling and Security Upgrade
Pipeline #167507 failed
......@@ -14,7 +14,7 @@
package org.opengroup.osdu.crs.middleware;
import io.swagger.jaxrs.config.JaxrsScanner;
import org.eclipse.jetty.http.BadMessageException;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.crs.util.AppError;
import org.opengroup.osdu.crs.util.AppException;
......@@ -38,6 +38,13 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
return this.getErrorResponse(e);
}
@ExceptionHandler(BadMessageException.class)
protected ResponseEntity<AppError> handleBadMessageException(BadMessageException e) {
/*When invalid input types are passed(recordId or dataId = "%"), it causes BadMessageException.
Handled it with generic error with code 400 (Bad Request).*/
return this.getErrorResponse(new AppException(BAD_REQUEST.value(), "Bad input type or format.", "Please check the input type and format and try again."));
}
@ExceptionHandler(Exception.class)
protected ResponseEntity<AppError> handleGeneralException(Exception e) {
return this.getErrorResponse(
......
package org.opengroup.osdu.crs.middleware;
import org.eclipse.jetty.http.BadMessageException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
......@@ -36,6 +37,19 @@ public class GlobalExceptionMapperTests {
Mockito.verify(log).error(Mockito.any(), Mockito.any(AppException.class));
}
@Test
public void should_useBadMassageValues_When_AppExceptionIsHandledByGlobalExceptionMapper() {
BadMessageException exception = new BadMessageException(400, "bad request reason");
ResponseEntity<AppError> response = sut.handleBadMessageException(exception);
AppError body = response.getBody();
assertNotNull(body);
assertEquals(400, body.getCode());
assertEquals("Please check the input type and format and try again.", body.getMessage());
assertEquals("Bad input type or format.", body.getReason());
Mockito.verify(log).error(Mockito.any(), Mockito.any(AppException.class));
}
@Test
public void should_useGenericValuesInResponse_When_ExceptionIsHandledByGlobalExceptionMapper() {
Exception exception = new Exception("any message");
......
......@@ -9,7 +9,7 @@
<docker.image.prefix>org.opengroup.osdu</docker.image.prefix>
<powermock.version>2.0.6</powermock.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<os-core-common.version>0.17.0</os-core-common.version>
<os-core-common.version>0.19.0-rc9</os-core-common.version>
<snakeyaml.version>1.33</snakeyaml.version>
<nimbus-jose-jwt.version>7.9</nimbus-jose-jwt.version>
<openapi.version>1.6.9</openapi.version>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment