11/*
2- * Copyright (c) 2024 Red Hat, Inc.
2+ * Copyright (c) 2024-2025 Red Hat, Inc.
33 * This program and the accompanying materials are made
44 * available under the terms of the Eclipse Public License 2.0
55 * which is available at https://www.eclipse.org/legal/epl-2.0/
1111 */
1212package com.redhat.devtools.gateway.openshift
1313
14+ import com.google.gson.Gson
1415import com.google.gson.reflect.TypeToken
16+ import com.intellij.openapi.diagnostic.thisLogger
1517import io.kubernetes.client.custom.V1Patch
1618import io.kubernetes.client.openapi.ApiClient
1719import io.kubernetes.client.openapi.ApiException
@@ -34,18 +36,35 @@ class DevWorkspaces(private val client: ApiClient) {
3436 @Throws(ApiException ::class )
3537 fun list (namespace : String ): List <DevWorkspace > {
3638 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()
4346
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+ }
4968 }
5069
5170 fun get (namespace : String , name : String ): DevWorkspace {
0 commit comments