From 080079cdd89f28c5d3edb87702e9e67a168f3d5b Mon Sep 17 00:00:00 2001 From: Anton Kalmykov <anton.kalmykov@proton.me> Date: Mon, 13 May 2024 06:38:41 +0000 Subject: [PATCH] Add a generic error message when the error response is not of JSON type Also add an errored server response content type check before parsing it as JSON Update related pot file Closes: https://gitlab.com/gitlab-org/gitlab/-/issues/438572 Changelog: changed --- .../graphql/helpers/resolver_helpers.js | 11 +++++++++++ locale/gitlab.pot | 3 +++ .../graphql/resolvers/kubernetes_spec.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+) 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 75a27ced8cd92..ee91ff7907463 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 b14d7a613c888..8fc2e48ec5c6d 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 335554020f94f..3fcbde665e316 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', () => { -- GitLab