From 8bebf2c126dad3b298434a6f6f42dfbe9dbcf165 Mon Sep 17 00:00:00 2001 From: Yohan Avishke Date: Wed, 23 Oct 2019 06:50:59 +0530 Subject: [PATCH] Wrap service level error messages from api level error messages --- .../jaxrs/exception/BadRequestException.java | 4 +++ .../InvalidExecutionPlanException.java | 4 +++ ...alyticsArtifactsManagementServiceImpl.java | 36 +++++++++---------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java index 6a09ee3af2b..0be9790e3a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/BadRequestException.java @@ -43,6 +43,10 @@ public class BadRequestException extends WebApplicationException { message = errorDTO.getMessage(); } + public BadRequestException(String message) { + this.message = message; + } + public String getMessage() { return message; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java index 8c453ba9cc1..61f1eacc0cd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/exception/InvalidExecutionPlanException.java @@ -39,6 +39,10 @@ public class InvalidExecutionPlanException extends WebApplicationException { message = errorDTO.getMessage(); } + public InvalidExecutionPlanException(String message) { + this.message = message; + } + @Override public String getMessage() { return message; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java index 623d9034004..1ca322f4423 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/AnalyticsArtifactsManagementServiceImpl.java @@ -119,8 +119,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployStream(stream); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy stream due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenant " + tenantDomain; log.error(errMsg, e); @@ -191,8 +192,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployReceiver(receiver, customMapping, adapterConfiguration); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy receiver due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -263,8 +265,9 @@ public class AnalyticsArtifactsManagementServiceImpl deployPublisher(publisher, customMapping, adapterConfiguration); return Response.ok().build(); } catch (BadRequestException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy publisher due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -295,8 +298,9 @@ public class AnalyticsArtifactsManagementServiceImpl deploySiddhiExecutionPlan(name, isEdited, plan.getDefinition()); return Response.ok().build(); } catch (InvalidExecutionPlanException e) { - log.error(e.getMessage(), e); - return e.getResponse(); + String errMsg = "Failed to deploy siddhi script due to invalid payload"; + log.error(errMsg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build(); } catch (AxisFault e) { String errMsg = "Failed to create event definitions for tenantDomain: " + tenantDomain; log.error(errMsg, e); @@ -634,9 +638,7 @@ public class AnalyticsArtifactsManagementServiceImpl eventProcessorAdminServiceStub.editActiveExecutionPlan(plan, name); } } else { - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(validationResponse); - throw new InvalidExecutionPlanException(errorDTO); + throw new InvalidExecutionPlanException(validationResponse); } } finally { cleanup(eventProcessorAdminServiceStub); @@ -655,9 +657,7 @@ public class AnalyticsArtifactsManagementServiceImpl String errMsg = String.format("Failed to validate Stream property attributes of %s:%s. " + "Stream mapping can't be null or empty", stream.getName(), stream.getVersion()); - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } } @@ -670,9 +670,7 @@ public class AnalyticsArtifactsManagementServiceImpl throws BadRequestException { if (adapterProperties == null) { String errMsg = "Failed to validate adapter attributes. Adapter attributes can't be null"; - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } } @@ -692,9 +690,7 @@ public class AnalyticsArtifactsManagementServiceImpl if (adapterMappingConfiguration == null) { String errMsg = "Failed to validate adapter mapping attributes. " + "Adapter mapping configuration can't be null"; - ErrorDTO errorDTO = new ErrorDTO(); - errorDTO.setMessage(errMsg); - throw new BadRequestException(errorDTO); + throw new BadRequestException(errMsg); } else if (adapterMappingConfiguration.getMessageFormat() == null || ((adapterMappingConfiguration.getInputMappingString() == null) && (adapterMappingConfiguration.getInputMappingProperties() == null ||