Skip to content

Commit

Permalink
Fix getter for error messages (#338)
Browse files Browse the repository at this point in the history
* fix: fix getter for error messages

Signed-off-by: Oleksii Orel <[email protected]>
  • Loading branch information
olexii4 authored Sep 9, 2021
1 parent 9efeb04 commit dd7f466
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
18 changes: 17 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 @@ -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);
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 dd7f466

Please sign in to comment.