Skip to content

Commit

Permalink
fix: fix getter for error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 committed Sep 9, 2021
1 parent fda8e7c commit 8dbdb3c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
14 changes: 13 additions & 1 deletion packages/common/src/helpers/__tests__/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -32,6 +32,18 @@ describe('Errors helper', () => {
expect(getMessage(undefined)).toEqual('Unexpected error.');
});

it('should check if it is an error or not', () => {
const message = 'Expected error.';
const error = new Error(message);
expect(isError(error)).toEqual(true);
expect(isError(message)).toEqual(false);
});

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);
Expand Down
4 changes: 4 additions & 0 deletions packages/common/src/helpers/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,7 @@ export class FactoryLoaderContainer extends React.PureComponent<Props, State> {
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) {
Expand Down

0 comments on commit 8dbdb3c

Please sign in to comment.