Skip to content

Commit 3b3beca

Browse files
committed
fix: refactoring for handling errors
1 parent 6e98699 commit 3b3beca

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/main/java/org/poolc/api/kubernetes/controller/KubernetesController.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.poolc.api.kubernetes.dto.GetMyKubernetesKeyResponseDto;
66
import org.poolc.api.kubernetes.service.KubernetesService;
77
import org.poolc.api.member.domain.Member;
8+
import org.springframework.http.HttpStatus;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.security.core.annotation.AuthenticationPrincipal;
1011
import org.springframework.web.bind.annotation.*;
@@ -36,10 +37,17 @@ public ResponseEntity<Void> refreshMamberKeys(@RequestHeader("X-API-KEY") String
3637

3738
@GetMapping(value="/me")
3839
public ResponseEntity<GetMyKubernetesKeyResponseDto> getMyKey(@AuthenticationPrincipal Member loginMember){
39-
GetMyKubernetesKeyResponseDto response = new GetMyKubernetesKeyResponseDto(
40-
kubernetesService.getKubernetesKeyByUUID(loginMember.getUUID())
41-
);
42-
return ResponseEntity.ok().body(response);
40+
if (loginMember == null) {
41+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
42+
}
43+
try {
44+
GetMyKubernetesKeyResponseDto response = new GetMyKubernetesKeyResponseDto(
45+
kubernetesService.getKubernetesKeyByUUID(loginMember.getUUID())
46+
);
47+
return ResponseEntity.ok().body(response);
48+
} catch (IllegalArgumentException e){
49+
return ResponseEntity.notFound().build();
50+
}
4351
}
4452

4553
}

src/main/java/org/poolc/api/kubernetes/service/KubernetesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void refreshMemberKey(Map<String,String> requestBody, String apiKey) {
4646

4747
public String getKubernetesKeyByUUID(String UUID) {
4848
return kubernetesRepository.findKubernetesKeyByUUID(UUID)
49-
.orElseThrow(() -> new IllegalArgumentException("No Kubernetes key found for user: " + UUID));
49+
.orElseThrow(() -> new IllegalArgumentException("No Kubernetes key found for the authenticated user"));
5050
}
5151

5252
private boolean isValidApiKey(String apiKey) {

0 commit comments

Comments
 (0)