1
1
/*
2
- * Copyright (c) 2024 Red Hat, Inc.
2
+ * Copyright (c) 2024-2025 Red Hat, Inc.
3
3
* This program and the accompanying materials are made
4
4
* available under the terms of the Eclipse Public License 2.0
5
5
* which is available at https://www.eclipse.org/legal/epl-2.0/
11
11
*/
12
12
package com.redhat.devtools.gateway.openshift
13
13
14
+ import com.google.gson.Gson
14
15
import com.google.gson.reflect.TypeToken
16
+ import com.intellij.openapi.diagnostic.thisLogger
15
17
import io.kubernetes.client.custom.V1Patch
16
18
import io.kubernetes.client.openapi.ApiClient
17
19
import io.kubernetes.client.openapi.ApiException
@@ -34,18 +36,35 @@ class DevWorkspaces(private val client: ApiClient) {
34
36
@Throws(ApiException ::class )
35
37
fun list (namespace : String ): List <DevWorkspace > {
36
38
val customApi = CustomObjectsApi (client)
37
- val response = customApi.listNamespacedCustomObject(
38
- " workspace.devfile.io" ,
39
- " v1alpha2" ,
40
- namespace,
41
- " devworkspaces"
42
- ).execute()
39
+ try {
40
+ val response = customApi.listNamespacedCustomObject(
41
+ " workspace.devfile.io" ,
42
+ " v1alpha2" ,
43
+ namespace,
44
+ " devworkspaces"
45
+ ).execute()
43
46
44
- val dwItems = Utils .getValue(response, arrayOf(" items" )) as List <* >
45
- return dwItems
46
- .stream()
47
- .map { dwItem -> DevWorkspace .from(dwItem) }
48
- .toList()
47
+ val dwItems = Utils .getValue(response, arrayOf(" items" )) as List <* >
48
+ return dwItems
49
+ .stream()
50
+ .map { dwItem -> DevWorkspace .from(dwItem) }
51
+ .toList()
52
+ } catch (e: ApiException ) {
53
+ thisLogger().info(e.message)
54
+
55
+ val response = Gson ().fromJson(e.responseBody, Map ::class .java)
56
+ // There might be some namespaces (OpenShift projects) in which the user cannot list resource "devworkspaces"
57
+ // e.g. "openshift-virtualization-os-images" on Red Hat Dev Sandbox, etc.
58
+ // It doesn't make sense to show an error to the user in such cases,
59
+ // so let's skip it silently.
60
+ if ((response[" code" ] as Double ) == 403.0 ) {
61
+ return emptyList()
62
+ } else {
63
+ // The error will be shown in the Gateway UI.
64
+ thisLogger().error(e.message)
65
+ throw e
66
+ }
67
+ }
49
68
}
50
69
51
70
fun get (namespace : String , name : String ): DevWorkspace {
0 commit comments