diff --git a/app/assets/javascripts/kubernetes_dashboard/graphql/helpers/resolver_helpers.js b/app/assets/javascripts/kubernetes_dashboard/graphql/helpers/resolver_helpers.js index 75a27ced8cd927f19483b2987ecc34bc375afc7a..ee91ff790746373ecd329c3b8f491d4720cf10cd 100644 --- a/app/assets/javascripts/kubernetes_dashboard/graphql/helpers/resolver_helpers.js +++ b/app/assets/javascripts/kubernetes_dashboard/graphql/helpers/resolver_helpers.js @@ -8,12 +8,23 @@ import { } from '@gitlab/cluster-client'; import { connectionStatus } from '~/environments/graphql/resolvers/kubernetes/constants'; import { updateConnectionStatus } from '~/environments/graphql/resolvers/kubernetes/k8s_connection_status'; +import { s__ } from '~/locale'; export const handleClusterError = async (err) => { if (!err.response) { throw err; } + const contentType = err.response.headers.get('Content-Type'); + + if (contentType !== 'application/json') { + throw new Error( + s__( + 'KubernetesDashboard|There was a problem fetching cluster information. Refresh the page and try again.', + ), + ); + } + const errorData = await err.response.json(); throw errorData; }; diff --git a/locale/gitlab.pot b/locale/gitlab.pot index b14d7a613c888c7165c0ba6dfe2f5d283a6f880b..8fc2e48ec5c6dec94c6249034d1efed6887d87ba 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -29847,6 +29847,9 @@ msgstr "" msgid "KubernetesDashboard|Suspended" msgstr "" +msgid "KubernetesDashboard|There was a problem fetching cluster information. Refresh the page and try again." +msgstr "" + msgid "KubernetesDashboard|View projects" msgstr "" diff --git a/spec/frontend/kubernetes_dashboard/graphql/resolvers/kubernetes_spec.js b/spec/frontend/kubernetes_dashboard/graphql/resolvers/kubernetes_spec.js index 335554020f94ffa4ed7c9c574ac7d47e81dfc7ea..3fcbde665e316c42bf4bb89380c360673847cd68 100644 --- a/spec/frontend/kubernetes_dashboard/graphql/resolvers/kubernetes_spec.js +++ b/spec/frontend/kubernetes_dashboard/graphql/resolvers/kubernetes_spec.js @@ -117,6 +117,20 @@ describe('~/frontend/environments/graphql/resolvers', () => { mockResolvers.Query.k8sDashboardPods(null, { configuration }, { client }), ).rejects.toThrow('API error'); }); + + it('should return a generic error message if the error response is not of JSON type', async () => { + jest.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces').mockRejectedValue({ + response: { + headers: new Headers({ 'Content-Type': 'application/pdf' }), + }, + }); + + await expect( + mockResolvers.Query.k8sDashboardPods(null, { configuration }, { client }), + ).rejects.toThrow( + 'There was a problem fetching cluster information. Refresh the page and try again.', + ); + }); }); describe('k8sDeployments', () => {