@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); }
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); } }