Skip to content

Commit

Permalink
Fixed parsedError string handling (#4266)
Browse files Browse the repository at this point in the history
* fixed parsedError string handling

* Added changelog

* Add unit test to devtools

Co-authored-by: Álex <[email protected]>
  • Loading branch information
asteriscos and AlexRuiz7 authored Jun 17, 2022
1 parent 2f83a43 commit 5979464
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Removed a logged error that appeared when the `statistics` tasks tried to create an index with the same name, causing the second task to fail on the creation of the index because it already exists [#4235](https://github.com/wazuh/wazuh-kibana-app/pull/4235)
- Fixed a UI crash due to a query with syntax errors in `Modules/Security events` [#4237](https://github.com/wazuh/wazuh-kibana-app/pull/4237)
- Fixed an error when generating a module report after changing the selected agent [#4240](https://github.com/wazuh/wazuh-kibana-app/pull/4240)
- Fixed an unhandled error when a Wazuh API request failed in the dev tools [#4266](https://github.com/wazuh/wazuh-kibana-app/pull/4266)
- Fixed an error related to `API not available` when saving the manager configuration and restarting the manager from `Management/Configuration/Edit configuration` on manager mode [#4264](https://github.com/wazuh/wazuh-kibana-app/pull/4264)
- Fixed a UI problem that required scrolling to see the logs in Management/Logs and Settings/Logs [#4253](https://github.com/wazuh/wazuh-kibana-app/pull/4253)

Expand Down
82 changes: 82 additions & 0 deletions public/controllers/dev-tools/dev-tools.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ErrorHandler } from '../../react-services';
import { DevToolsController } from './dev-tools';

// mocked some required kibana-services
jest.mock('../../kibana-services', () => ({
...(jest.requireActual('../../kibana-services') as object),
getUiSettings: jest.fn().mockReturnValue({
get: (name) => {
return true;
},
}),
}));

// mocked window object
Object.defineProperty(window, 'location', {
value: {
hash: {
endsWith: jest.fn(),
includes: jest.fn(),
},
href: jest.fn(),
assign: jest.fn(),
search: jest.fn().mockResolvedValue({
tab: 'api',
}),
path: jest.fn(),
},
writable: true,
});
// mocked scope dependency
const $scope = {
$applyAsync: jest.fn(),
};
// mocked getErrorOrchestrator
const mockedGetErrorOrchestrator = {
handleError: jest.fn(),
};

jest.mock('../../react-services/common-services', () => {
return {
getErrorOrchestrator: () => mockedGetErrorOrchestrator,
};
});

describe('Devtools Controller', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('Devtools.Send()', () => {
it('Should return string type errors to print in the codemirror textarea output', async () => {

// Create new instance of the devtools controller
const controller = new DevToolsController($scope, window, ErrorHandler, [window.document]);

// Define possible error structures to parse
const errorTypes = {
dataObject: {
data: { message: 'Data Object Error' }
},
invalidObject: {
error: 'Invalid Object Error'
},
object: {
data: 'Object Error',
status: -1
},
string: "String Error",
null: null,
undefined: undefined,
number: 1,
}

expect(controller.parseError(errorTypes.object)).toEqual('Wazuh API is not reachable. Reason: timeout.');
expect(controller.parseError(errorTypes.string)).toEqual('String Error');
expect(controller.parseError(errorTypes.dataObject)).toEqual(errorTypes.dataObject.data.message);
expect(controller.parseError(errorTypes.invalidObject)).toEqual(JSON.stringify(errorTypes.invalidObject));

})
})

});
30 changes: 16 additions & 14 deletions public/controllers/dev-tools/dev-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ export class DevToolsController {
$(this.$document[0]).keyup(e => {
this.multipleKeyPressed = [];
});

const element = this.$document[0].getElementById('api_input');
this.apiInputBox = CodeMirror.fromTextArea(
this.$document[0].getElementById('api_input'),
element,
{
lineNumbers: true,
matchBrackets: true,
Expand Down Expand Up @@ -831,19 +831,21 @@ export class DevToolsController {
};
getErrorOrchestrator().handleError(options);

if ((error || {}).status === -1) {
return this.apiOutputBox.setValue(
'Wazuh API is not reachable. Reason: timeout.'
);
return this.apiOutputBox.setValue(this.parseError(error));
}
}

parseError(error){
if ((error || {}).status === -1) {
return 'Wazuh API is not reachable. Reason: timeout.';
} else {
const parsedError = ErrorHandler.handle(error, '', { silent: true });
if (typeof parsedError === 'string') {
return parsedError;
} else if (error && error.data && typeof error.data === 'object') {
return JSON.stringify(error);
} else {
const parsedError = ErrorHandler.handle(error, '', { silent: true });
if (typeof parsedError === 'string') {
return this.apiOutputBox.setValue(error);
} else if (error && error.data && typeof error.data === 'object') {
return this.apiOutputBox.setValue(JSON.stringify(error));
} else {
return this.apiOutputBox.setValue('Empty');
}
return 'Empty';
}
}
}
Expand Down

0 comments on commit 5979464

Please sign in to comment.