소스 검색

Exception handling (1)

dagarcos 2 년 전
부모
커밋
09b7467136
1개의 변경된 파일11개의 추가작업 그리고 5개의 파일을 삭제
  1. 11 5
      DBCDS_S10_1_Api/src/main/java/es/uv/garcosda/api/controllers/VideosController.java

+ 11 - 5
DBCDS_S10_1_Api/src/main/java/es/uv/garcosda/api/controllers/VideosController.java

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.ResourceAccessException;
 import org.springframework.web.client.RestTemplate;
 
 import es.uv.garcosda.shared.Video;
@@ -46,12 +47,17 @@ public class VideosController {
 			url+="?userId="+userId.get();
 		}	
 		
-		ResponseEntity<Video[]> response = template.getForEntity(url, Video[].class); 
-		if(response.getStatusCode() == HttpStatus.OK) {
-			List<Video> videos = Arrays.stream(response.getBody()).collect(Collectors.toList());
-			return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
+		try {
+			ResponseEntity<Video[]> response = template.getForEntity(url, Video[].class); 
+			if(response.getStatusCode() == HttpStatus.OK) {
+				List<Video> videos = Arrays.stream(response.getBody()).collect(Collectors.toList());
+				return new ResponseEntity<List<Video>>(videos, HttpStatus.OK);
+			}
+			return new ResponseEntity<String>("API returned no content", HttpStatus.SERVICE_UNAVAILABLE);
 		}
-		return new ResponseEntity<String>("API returned no content", HttpStatus.SERVICE_UNAVAILABLE);		
+		catch(ResourceAccessException e){ }
+		
+		return new ResponseEntity<String>("Cannot connect to repository", HttpStatus.SERVICE_UNAVAILABLE);		
 	}
 	
 	@GetMapping("{id}")