From dd7f466600d5df94304ac5b15e47a0af816fefce Mon Sep 17 00:00:00 2001 From: Oleksii Orel Date: Thu, 9 Sep 2021 12:33:17 +0300 Subject: [PATCH] Fix getter for error messages (#338) * fix: fix getter for error messages Signed-off-by: Oleksii Orel --- .../src/helpers/__tests__/errors.spec.ts | 18 +++++++++++++++++- packages/common/src/helpers/errors.ts | 4 ++++ .../src/containers/FactoryLoader/index.tsx | 7 +------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/packages/common/src/helpers/__tests__/errors.spec.ts b/packages/common/src/helpers/__tests__/errors.spec.ts index 70149c50a..55224c5a0 100644 --- a/packages/common/src/helpers/__tests__/errors.spec.ts +++ b/packages/common/src/helpers/__tests__/errors.spec.ts @@ -14,7 +14,7 @@ import axios, { AxiosError, AxiosResponse } from 'axios'; import { HttpError } from '@kubernetes/client-node'; import AxiosMockAdapter from 'axios-mock-adapter'; import * as http from 'http'; -import { getMessage } from '../errors'; +import { getMessage, isError } from '../errors'; let mockAxios = new AxiosMockAdapter(axios); @@ -28,10 +28,26 @@ describe('Errors helper', () => { mockAxios.resetHandlers(); }); + describe('Typeguards', () => { + + it('should check if error', () => { + const message = 'Expected error.'; + const error = new Error(message); + expect(isError(error)).toEqual(true); + expect(isError(message)).toEqual(false); + }); + + }); + it('should return default message', () => { expect(getMessage(undefined)).toEqual('Unexpected error.'); }); + it('should return message', () => { + const message = 'Expected error message.'; + expect(getMessage(message)).toEqual(message); + }); + it('should return error message', () => { const message = 'Expected error.'; const error = new Error(message); diff --git a/packages/common/src/helpers/errors.ts b/packages/common/src/helpers/errors.ts index 8c1f64f21..ec39155a4 100644 --- a/packages/common/src/helpers/errors.ts +++ b/packages/common/src/helpers/errors.ts @@ -54,6 +54,10 @@ export function getMessage(error: unknown): string { return error.message; } + if (typeof (error) === 'string') { + return error; + } + console.error('Unexpected error:', error); return 'Unexpected error. Check DevTools console and network tabs for more information.' } diff --git a/packages/dashboard-frontend/src/containers/FactoryLoader/index.tsx b/packages/dashboard-frontend/src/containers/FactoryLoader/index.tsx index a5e09fba6..3d1135663 100644 --- a/packages/dashboard-frontend/src/containers/FactoryLoader/index.tsx +++ b/packages/dashboard-frontend/src/containers/FactoryLoader/index.tsx @@ -268,12 +268,7 @@ export class FactoryLoaderContainer extends React.PureComponent { this.resolvePrivateDevfile(e.attributes.oauth_authentication_url, location); return; } - - let alertMessage = 'Failed to resolve a devfile.'; - if (common.helpers.errors.isError(e)) { - alertMessage += ' ' + common.helpers.errors.getMessage(e); - } - this.showAlert(alertMessage); + this.showAlert(`Failed to resolve a devfile. ${common.helpers.errors.getMessage(e)}`); return; } if (this.factoryResolver.resolver?.location !== location) {