wazuh-dashboard-plugins icon indicating copy to clipboard operation
wazuh-dashboard-plugins copied to clipboard

Global error management on failed requests

Open Machi3mfl opened this issue 2 years ago • 10 comments

Wazuh Elastic Rev Security
4.x 7.x 4xxx Basic, ODFE, Xpack
Browser
Chrome, Firefox, Safari, etc

Description

On this issue, we will analyze all the error treatments. We need to find all the cases where we catch errors and decide how to throw them to other application methods.

This error was found when an error toast is shown and then the "Se full error" button is clicked, but doesn't show the error description.

Cases:

  1. https://github.com/wazuh/wazuh-kibana-app/issues/4030#issuecomment-1124838582

imagen

Both versions environments: 4.3.0. Both App revision: 4301-1.

  • Ways to replicate the error in cloud environment:

    1. Restarting the service and going to Wazuh > Settings > API Configuration. This will prompt the button but when clicking on it the error in the console triggers.
    2. Restarting the service and going to Wazuh > Modules > Any module. This will prompt the button but when clicking on it the error in the console triggers.
    3. If the configuration file is empty and we click in Save button. This will prompt the button but when clicking on it the error in the console triggers.
  • Way to replicate in OVA

    1. Restarting the service and going to Wazuh > Settings > API Configuration. This will prompt the button but when clicking on it the error in the console triggers.

Thanks @jhaos

Machi3mfl avatar May 13 '22 16:05 Machi3mfl

Steps

  • [X] 1. Tracking all error throw errors that don't return an error instance
  • [x] 2. Searching those methods usage (previous step)
  • [x] 3. Analyzing possible solutions, best error treatment
  • [x] 4. Implement solution
  • [x] 5. Test solutions
# File Unit Test Files  Status
1 /public/react-services/wz-api-check.js Added
2 /public/react-services/wz-request.ts Added
3 /public/react-services/generic-request.js Added
4 /public/react-services/saved-objects.js Added
5 /public/components/health-check/services/check-api.service.ts Added
6 /public/controllers/settings/settings.js Added

Actual services relationship

graph TD;
    check-api.service-->generic-request;
    check-api.service-->wz-api-check;
    saved-objects-->generic-request;
    wz-request-->axios
    generic-request-->axios
    wz-api-check-->axios

Machi3mfl avatar May 16 '22 14:05 Machi3mfl

1. wz-api-check: public/react-services/wz-api-check.js

checkStored method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-api-check.js#L50-L61

Use trace

checkApiService

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/health-check/services/check-api.service.ts#L63-L71

componentDidMount

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/wz-menu/wz-menu.js#L93-L124

request

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/generic-request.js#L92-L114

genericReq

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/generic-request.js#L92-L114

checkApi method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-api-check.js#L89-L99

Use trace

trySetDefault

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/health-check/services/check-api.service.ts#L20-L48

ChangeAPI

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/wz-menu/wz-menu.js#L348-L382

onInit

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/controllers/settings/settings.js#L70-L102

tryToSetDefault

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/services/resolves/settings-wizard.js#L125-L149

Machi3mfl avatar May 17 '22 15:05 Machi3mfl

2. wz-request: public/react-services/wz-request.ts

genericReq method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-request.ts#L62-L99

Use trace

componentDidMount

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/add-modules-data/sample-data.tsx#L80-L105

addSampleData

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/add-modules-data/sample-data.tsx#L146-L189

removeSampleData

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/add-modules-data/sample-data.tsx#L192-L228

editKey

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/settings/configuration/utils/configuration-handler.js#L21-L32

SampleDataWarning

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/visualize/components/sample-data-warning.js#L24-L40

checkCurrentSecurityPlatform

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/controllers/management/components/management/configuration/utils/wz-fetch.js#L511-L522

ReportingHandler

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/controllers/management/components/management/reporting/utils/reporting-handler.js#L20-L44

existStatisticsIndices

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/controllers/management/components/management/statistics/statistics-overview.js#L266-L283

startVis2Png

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/reporting.js#L116-L144

startConfigReport

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/reporting.js#L168-L198

login

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-authentication.ts#L40-L53

csvReq method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-request.ts#L149-L153

Use trace

exportCsv

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-csv.js#L23-L30

apiReq method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/wz-request.ts#L118-L132

Use trace

apiReq is the most used app method, we need to check its use careful

Machi3mfl avatar May 17 '22 15:05 Machi3mfl

3. generic-request: /public/react-services/generic-request.js

request method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/generic-request.js#L92-L115

Use trace

request method (GenericRequest.request) is used on 17 files and 40 times.

Machi3mfl avatar May 17 '22 15:05 Machi3mfl

4. saved-objects: /public/react-services/saved-objects.js

existsIndexPattern method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L137-L141

Use trace

getExistingIndexPattern method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L172-L179

Use trace

createSavedObject method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L195-L199

Use trace

refreshFieldsOfIndexPattern method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L217-L221

Use trace

refreshIndexPattern

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L238-L243

createWazuhIndexPattern

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/react-services/saved-objects.js#L292-L296

Machi3mfl avatar May 17 '22 15:05 Machi3mfl

5. check-api.service: /public/components/health-check/services/check-api.service.ts

trySetDefault method

https://github.com/wazuh/wazuh-kibana-app/blob/aa1a8e41a4337610889dd0332c63a1214ce91582/public/components/health-check/services/check-api.service.ts#L40-L48

Machi3mfl avatar May 17 '22 15:05 Machi3mfl

Applied solution:

  • [x] Created ErrorFactory

Flow with Error Factory Implementation

graph TD;
 generic-request-->ThrowError
 wz-request-->ThrowError
 wz-api-check-->ThrowError
 ThrowError-->ErrorFactory
 ErrorFactory-->Error
 Error-->ErrorOrchestrator
 ErrorOrchestrator-->UI
 ErrorOrchestrator-->Browser-Console
 ErrorOrchestrator-->Logs

Iteration steps to cover all services and cases

  1. Use ErrorFactory createError in services
  2. Add unit tests

Base Services

  • [x] generic-request
  • [x] wz-request
  • [x] wz-api-check

Services

  • [x] check-api-service
  • [x] saved-objects

Machi3mfl avatar May 24 '22 17:05 Machi3mfl

This PR also resolves issue #4030 by implementing the use of the new Error Factory feature on root services

  • Added SettingsController tests to check the controller catches an Error instance instead of a string.

Issue screenshot

image

Solution screenshot

https://user-images.githubusercontent.com/6089438/171688212-d0f9d71b-326a-4157-8c03-c2f9a870ab20.mov

For testing

  • Restarting the service and going to Wazuh > Settings > API Configuration.
  • Click on the error toast "See full error" button and check if the error detail is shown.
  • Check the navigator console.

Machi3mfl avatar Jun 02 '22 17:06 Machi3mfl

Flow with Error Factory Implementation

graph TD;
 generic-request-->ThrowError
 wz-request-->ThrowError
 wz-api-check-->ThrowError
 ThrowError-->ErrorFactory
 ErrorFactory-->Error
 Error-->ErrorOrchestrator
 ErrorOrchestrator-->UI
 ErrorOrchestrator-->Browser-Console
 ErrorOrchestrator-->Logs

Advantages

  • Centralize the error management for the errors generated by the services
  • Catch all errors generated and guarantee to throw the correct error format for showing at UI
  • Add the capability to identify and create specific errors for all known error cases

Machi3mfl avatar Jun 27 '22 16:06 Machi3mfl

Next steps

  • [x] Identify error types received by the error factory
  • [x] Create Errors classes for specific errors
  • [x] Implement error treatment by each error type

Generic Error Types

EvalError

Creates an instance representing an error that occurs regarding the global function eval().

RangeError

Creates an instance representing an error that occurs when a numeric variable or parameter is outside its valid range.

ReferenceError

Creates an instance representing an error that occurs when de-referencing an invalid reference.

SyntaxError

Creates an instance representing a syntax error.

TypeError

Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.

URIError

Creates an instance representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters.

AggregateError

Creates an instance representing several errors wrapped in a single error when multiple errors need to be reported by an operation, for example by Promise.any().

InternalError Non-Standard

Creates an instance representing an error that occurs when an internal error in the JavaScript engine is thrown. E.g. "too much recursion".

Error sources

  • Wazuh API errors
  • Elasticsearch errors
  • Operational errors (development)
  • Axios errors

Machi3mfl avatar Jun 27 '22 16:06 Machi3mfl