Skip to content
This repository has been archived by the owner on Mar 11, 2023. It is now read-only.

Commit

Permalink
Global exception handling for controller layer added using @Controlle…
Browse files Browse the repository at this point in the history
…rAdvice
  • Loading branch information
vnkdj5 committed Mar 24, 2019
1 parent 6818fff commit 3212b61
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 28 deletions.
28 changes: 21 additions & 7 deletions src/main/java/com/workflow/controller/ComponentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileReader;
import java.util.*;

import com.workflow.exceptions.NoDataFoundException;
import com.workflow.service.ComponentService;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
Expand Down Expand Up @@ -41,14 +42,18 @@ public class ComponentController {

@RequestMapping(value="/getConfig/{WFId}/{componentId}", method= RequestMethod.GET)
public ResponseEntity<HashMap> getConfig(@PathVariable("WFId") String WFId, @PathVariable("componentId") String CId){

Entity config = componentService.getConfig(WFId, CId);
Entity config;
try {
config = componentService.getConfig(WFId, CId);
} catch (Exception ex) {
throw new NoDataFoundException(WFId, CId, "Configuration not found");
}
//add config model to the response and send back the entity.
if(config==null) {

HashMap obj=new HashMap<String,String>();
throw new NoDataFoundException(WFId, CId, "Configuration not found");
/*HashMap obj=new HashMap<String,String>();
obj.put("message","No config found");
return new ResponseEntity<>(obj,HttpStatus.INTERNAL_SERVER_ERROR);
return new ResponseEntity<>(obj,HttpStatus.INTERNAL_SERVER_ERROR);*/
}else {
return new ResponseEntity<>(config.getEntity(),HttpStatus.OK);
}
Expand Down Expand Up @@ -87,15 +92,24 @@ public ResponseEntity<HashMap> setConfig(@RequestBody JSONObject config, @PathVa

@RequestMapping(value="/getInput/{WFId}/{componentId}", method=RequestMethod.GET)
public ResponseEntity<ArrayList<JSONObject>> getInput(@PathVariable("WFId") String WFId, @PathVariable("componentId") String CId){
Entity response=componentService.getInput(WFId, CId);
Entity response;
try {
response = componentService.getInput(WFId, CId);
} catch (RuntimeException ex) {
System.out.println("Get input error");
throw new NoDataFoundException(WFId, CId, "No input present ");
}
if (response == null) {
throw new NoDataFoundException(WFId, CId, "No input present ");
}
return new ResponseEntity<>((ArrayList<JSONObject>)response.getEntity().get("input"),HttpStatus.OK);
}

@RequestMapping(value="/getOutput/{WFId}/{componentId}", method=RequestMethod.GET)
public ResponseEntity<ArrayList<JSONObject>> getOutput(@PathVariable("WFId") String WFId, @PathVariable("componentId") String CId){
Entity response=componentService.getOutput(WFId, CId);
if(response==null){
return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
throw new NoDataFoundException(WFId, CId, "No output present ");
}
return new ResponseEntity<>((ArrayList<JSONObject>)response.getEntity().get("output"),HttpStatus.OK);
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/workflow/controller/GraphController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Date;

import com.workflow.bean.GraphLink;
import com.workflow.exceptions.GenericRuntimeException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand Down Expand Up @@ -44,7 +45,8 @@ public ResponseEntity<String> saveWorkflow(@RequestBody List<JSONObject> updateL
}
}catch(Exception e) {
e.printStackTrace();
return new ResponseEntity<String>("{\"message\":\"Workflow Save Error! Try Again\"}",HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException(WFId, "Workflow save error. Try again after some time.");
//return new ResponseEntity<String>("{\"message\":\"Workflow Save Error! Try Again\"}",HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<String>("{\"message\":\"Workflow Saved Successfully\"}",HttpStatus.OK);
Expand All @@ -61,6 +63,8 @@ public ResponseEntity<HashMap> createWorkflow(@RequestBody String name) {
e.printStackTrace();
}
HashMap<String,Object> map = graphService.newWorkflow(iname);
if (map == null)
throw new GenericRuntimeException(name, "Error while creating workflow");
if(map!=null && (Boolean)map.get("Found")) {
return new ResponseEntity<HashMap>(map,HttpStatus.FOUND);
}else {
Expand Down Expand Up @@ -92,7 +96,8 @@ public ResponseEntity<String> deleteWorkflow(@PathVariable("WFId") String id) {
try {
graphService.deleteGraph(id);
}catch(Exception e) {
return new ResponseEntity<String>("{\"message\":\"Workflow Deletion Error! Try Again\"}",HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException(id, "Workflow Deletion Error! Try Again");
//return new ResponseEntity<String>("{\"message\":\"Workflow Deletion Error! Try Again\"}",HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<String>("{\"message\":\"Workflow Deleted Successfully\"}",HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public ResponseEntity<HashMap> runWorkflow(@PathVariable("WFId") String WFId){

}

/*
Explicitly checking status of execution by execution by sending REST Request
Need to add status as a variable in Workflow class where current status of execution will be stored
Not recommended.
P.S. Replaced with web socket implementation
*/
@RequestMapping(value = "/executionStatus/{WFId}", method = RequestMethod.GET)
public ResponseEntity<HashMap> getStatus(@PathVariable("WFId") String WFId) {
HashMap<String, String> status = new HashMap<>();
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/workflow/controller/UtilityController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.workflow.controller;

import com.workflow.component.Entity;
import com.workflow.exceptions.GenericRuntimeException;
import com.workflow.service.ComponentService;
import com.workflow.service.Helper;
import org.json.JSONArray;
Expand Down Expand Up @@ -79,21 +80,17 @@ public ResponseEntity<HashMap> uploadFile(@RequestParam("file") MultipartFile fi
return new ResponseEntity<HashMap>(model.getEntity(), HttpStatus.OK);
}
else {
model.getEntity().put("message", "Incompatible headers");
return new ResponseEntity<HashMap>(model.getEntity(),HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException(CompId, "Incompatible headers");
}


} catch (Exception e) {
e.printStackTrace();
map.put("message", "File Upload exception");
return new ResponseEntity<HashMap>(map,HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException(CompId, "File upload exception.");
}
} else {
//return "You failed to upload " + name
// + " because the file was empty.";
map.put("message", "File Uploade error");
return new ResponseEntity<HashMap>(map,HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException(CompId, "File upload error at server.");

}

//return new ResponseEntity<String>("{\"message\":\"File Uploaded successfully\"}",HttpStatus.OK);
Expand All @@ -105,7 +102,7 @@ public ResponseEntity<String> deleteFile(@RequestBody HashMap data){
if(file.delete()) {
return new ResponseEntity<String>("{\"message\":\"File deleted\"}",HttpStatus.OK);
}
return new ResponseEntity<String>("{\"message\":\"Cannot delete file\"}",HttpStatus.INTERNAL_SERVER_ERROR);
throw new GenericRuntimeException("File deletion failed.");
}

@RequestMapping(value="/testQuery/{WFId}/{compId}", method=RequestMethod.GET)
Expand All @@ -116,8 +113,8 @@ public ResponseEntity<ArrayList<String>> testQuery(@PathVariable("WFId")String W
if(ret==null)
ret.add("Invalid request");
}catch (Exception e){
ret.add("Error in processing query");
return new ResponseEntity<>(ret,HttpStatus.INTERNAL_SERVER_ERROR);

throw new GenericRuntimeException(CompId, "Error in processing query");
}
return new ResponseEntity<>(ret,HttpStatus.OK);
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/workflow/exceptions/GenericRuntimeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.workflow.exceptions;

import java.util.Date;

public class GenericRuntimeException extends RuntimeException {
private Date timestamp;
private String message;

public GenericRuntimeException(String WFId, String name, String notAvailable) {
this.timestamp = new Date();
this.message = WFId + " " + name + "-" + notAvailable;
}

public GenericRuntimeException(String WFId, String msg) {
this.timestamp = new Date();
this.message = WFId + " : " + msg;
}

public GenericRuntimeException(String message) {
this.message = message;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}


}
18 changes: 15 additions & 3 deletions src/main/java/com/workflow/exceptions/NoDataFoundException.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
public class NoDataFoundException extends RuntimeException {

private String message;
private String WFId;
private String CId;

public NoDataFoundException(String description)
public String getWFId() {
return WFId;
}

public void setWFId(String WFId) {
this.WFId = WFId;
}

public NoDataFoundException(String WFId, String CId, String description)
{
this.message= description;
this.message = description;
this.CId = CId;
this.WFId = WFId;
}

public String getMessage() {
return message;
return message + " for " + CId + ".";
}

public void setMessage(String message) {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/workflow/exceptions/RestExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.workflow.exceptions;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

// other exception handlers

@ExceptionHandler(NoDataFoundException.class)
protected ResponseEntity<Object> handleEntityNotFound(NoDataFoundException ex) {
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex);
apiError.setMessage(ex.getMessage());
return buildResponseEntity(apiError);
}

@ExceptionHandler(GenericRuntimeException.class)
protected ResponseEntity<Object> handleGenericException(GenericRuntimeException ex) {
ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex);
apiError.setMessage(ex.getMessage());
return buildResponseEntity(apiError);
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> baseException(Exception ex) {
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex);
apiError.setMessage(ex.getMessage());
return buildResponseEntity(apiError);
}

@ExceptionHandler(RuntimeException.class)
protected ResponseEntity<Object> baseRunTimeException(Exception ex) {
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex);
apiError.setMessage(ex.getMessage());
return buildResponseEntity(apiError);
}

private ResponseEntity<Object> buildResponseEntity(ApiError apiError) {
return new ResponseEntity<>(apiError, apiError.getStatus());
}
}
10 changes: 5 additions & 5 deletions src/main/webapp/scripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ app.controller('DiagramCtrl', ['$scope', '$rootScope', 'fileUpload', 'graphServi
//Add notification show "Component not found"
$scope.schema = null;
$scope.form = null;
$scope.form.model = null;
notify.showError("Error!", response.data);
// $scope.form.model = null;
notify.showError("Error!", response.data.message);

});
};
Expand Down Expand Up @@ -1050,7 +1050,7 @@ app.controller('DiagramCtrl', ['$scope', '$rootScope', 'fileUpload', 'graphServi

},
function onError(error) {
notify.showError("Error", error.statusText);
notify.showError("Error", error.data.message);
return null;
}
)
Expand Down Expand Up @@ -1126,7 +1126,7 @@ app.controller('DiagramCtrl', ['$scope', '$rootScope', 'fileUpload', 'graphServi
$('#inputModal').modal('show');
},
function error(response) {
notify.showError("Error!!", response.statusText);
notify.showError("Error!!", response.data.message);
}
);
} else {
Expand Down Expand Up @@ -1185,7 +1185,7 @@ app.controller('DiagramCtrl', ['$scope', '$rootScope', 'fileUpload', 'graphServi
$('#inputModal').modal('show');
},
function error(response) {
notify.showError("Error!!", response.statusText);
notify.showError("Error!!", response.data.message);
}
)
}
Expand Down

0 comments on commit 3212b61

Please sign in to comment.