Skip to content
Snippets Groups Projects
Commit f2b35173 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

error reporting fixedd (proper json is sent to the client)

parent d9654bd5
No related branches found
No related tags found
1 merge request!195Resolve "data overlay with miriam ids"
......@@ -18,6 +18,7 @@ import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidStateException;
......@@ -30,30 +31,30 @@ public abstract class BaseController {
@ExceptionHandler({ Exception.class })
public ResponseEntity<Object> handleException(Exception e, WebRequest request) {
if (e instanceof lcsb.mapviewer.services.SecurityException) {
return new ResponseEntity<Object>("{\"error\" : \"Access denied.\",\"reason\":\"" + e.getMessage() + "\"}",
new HttpHeaders(), HttpStatus.FORBIDDEN);
return createErrorResponse("Access denied.", e.getMessage(), new HttpHeaders(), HttpStatus.FORBIDDEN);
} else if (e instanceof ObjectNotFoundException) {
return new ResponseEntity<Object>("{\"error\" : \"Object not found.\",\"reason\":\"" + e.getMessage() + "\"}",
new HttpHeaders(), HttpStatus.NOT_FOUND);
return createErrorResponse("Object not found.", e.getMessage(), new HttpHeaders(), HttpStatus.NOT_FOUND);
} else if (e instanceof ObjectExistsException) {
return new ResponseEntity<Object>(
"{\"error\" : \"Object already exists.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(),
HttpStatus.CONFLICT);
return createErrorResponse("Object already exists.", e.getMessage(), new HttpHeaders(), HttpStatus.CONFLICT);
} else if (e instanceof QueryException) {
logger.error(e, e);
return new ResponseEntity<Object>("{\"error\" : \"Query server error.\",\"reason\":\"" + e.getMessage() + "\"}",
new HttpHeaders(), HttpStatus.BAD_REQUEST);
return createErrorResponse("Query server error.", e.getMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST);
} else if (e instanceof ServletRequestBindingException && e.getMessage().indexOf(Configuration.AUTH_TOKEN) >= 0) {
return new ResponseEntity<Object>("{\"error\" : \"Access denied.\",\"reason\":\"" + e.getMessage() + "\"}",
new HttpHeaders(), HttpStatus.FORBIDDEN);
return createErrorResponse("Access denied.", e.getMessage(), new HttpHeaders(), HttpStatus.FORBIDDEN);
} else {
logger.error(e, e);
return new ResponseEntity<Object>(
"{\"error\" : \"Internal server error.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(),
return createErrorResponse("Internal server error.", e.getMessage(), new HttpHeaders(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private ResponseEntity<Object> createErrorResponse(String errorMessage, String error, HttpHeaders httpHeaders,
HttpStatus status) {
return new ResponseEntity<Object>(
"{\"error\" : \"" + errorMessage + "\",\"reason\":" + new Gson().toJson(error) + "}", httpHeaders, status);
}
public Map<String, Object> parseBody(String body) throws IOException, JsonParseException, JsonMappingException {
if (body == null || body.isEmpty()) {
return new HashMap<>();
......
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