diff --git a/CHANGELOG.md b/CHANGELOG.md index bd11ff6db4..a2ca353c4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/public/controllers/dev-tools/dev-tools.test.tsx b/public/controllers/dev-tools/dev-tools.test.tsx new file mode 100644 index 0000000000..5559c6d19a --- /dev/null +++ b/public/controllers/dev-tools/dev-tools.test.tsx @@ -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)); + + }) + }) + +}); \ No newline at end of file diff --git a/public/controllers/dev-tools/dev-tools.ts b/public/controllers/dev-tools/dev-tools.ts index f00e229007..d35b624419 100644 --- a/public/controllers/dev-tools/dev-tools.ts +++ b/public/controllers/dev-tools/dev-tools.ts @@ -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, @@ -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'; } } }