@ExceptionHandler(value = Exception.class) public ResponseEntity<ErrorEntity> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { LOG.error("Intercepted exception: {} for URL: {}", e.getMessage(), req.getRequestURL()); LOG.error("Exception stacktrace:", e); if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } return new ResponseEntity<>(new ErrorEntity(500, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); }
private static void handleError(ErrorEntity errorEntity, HttpServletResponse response) { sendResponse(errorEntity, errorEntity.getStatusCode(), response); }
private ClientResponse handleResponse(ClientResponse response) throws RemoteException { if(Range.closedOpen(200, 400).contains(response.getStatus())) { return response; } else if(Range.closedOpen(400, 500).contains(response.getStatus())) { String responseEntity = response.getEntityAsString(); if(LOG.isDebugEnabled()) { LOG.debug("Response: {}", responseEntity); } try { String message = ""; if(responseEntity != null) { ErrorEntity errorEntity = new JsonRestResponseHandler().deserialize(ErrorEntity.class, responseEntity); message = errorEntity.getMessage(); } if(response.getStatus() == HttpStatus.NOT_FOUND.value()) { throw new ResourceNotFoundException("No resource was found, " + message); } else { LOG.error("Raw response: {}", responseEntity); throw new RemoteException("Unable to execute remote operation: " + message + " statuscode: " + response.getStatus()); } } catch(RestException e) { String reason = HttpStatus.valueOf(response.getStatus()).getReasonPhrase(); throw new RemoteException("Unable to execute remote operation: " + response.getStatus() + "(" + reason + ")"); } } else { String responseEntity = response.getEntityAsString(); LOG.error("Remote server response with an error: {}", responseEntity); throw new RemoteException("Unable to execute remote operation: " + response.getStatus()); } }
public static void handleError(String message, HttpServletResponse response) { handleError(new ErrorEntity(HttpStatus.BAD_REQUEST.value(), message), response); }
private RestEntity requestById(StorageService storageService, String requestedId, RequestContext context) throws JasDBStorageException { Entity entity = storageService.getEntityById(context, requestedId); if(entity != null) { return new StreamedEntity(entity); } else { return new ErrorEntity(404, "No entity was found with id: " + requestedId); } }
public static void createResponse(final RestEntity entity, HttpServletResponse response) { if(entity != null) { if(entity instanceof ErrorEntity) { handleError((ErrorEntity)entity, response); } else { sendResponse(entity, HttpStatus.OK.value(), response); } } else { handleError(new ErrorEntity(HttpStatus.NOT_FOUND.value(), "Resource could not be found"), response); } }
private ResponseEntity<RestEntity> doSearch(String instanceId, String bagName) throws JasDBException { DBInstance instance = getInstance(instanceFactory, instanceId); LOG.debug("Searching for a bag on instance: {} with name: {}", instanceId, bagName); try { StorageService storageService = storageServiceFactory.getStorageService(instance.getInstanceId(), bagName); if(storageService != null) { LOG.debug("Found a bag with name: {}", bagName); return ok(new RestBag(instance.getInstanceId(), bagName, storageService.getSize(), storageService.getDiskSize())); } else { return notFound(new ErrorEntity(404, "No bag was found with name: " + bagName)); } } catch(JasDBStorageException e) { throw new RestException("Unable to load bag metadata", e); } }