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

Fixed catch error throw string instead error

Open Machi3mfl opened this issue 2 years ago • 1 comments

This PR, Resolves and implements a new solution to catch that throw errors. We need to prevent and ensure that all the errors caught are error instances.

#4145

Machi3mfl avatar May 23 '22 11:05 Machi3mfl

Jest Test Coverage % values
Statements 4.8% ( 1745 / 36378 )
Branches 2.23% ( 628 / 28120 )
Functions 3.45% ( 307 / 8900 )
Lines 4.86% ( 1693 / 34800 )

github-actions[bot] avatar Jun 02 '22 18:06 github-actions[bot]

Error sources

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

Error management components

Error factory

The error factory is responsible to create different instances of error depending on the parameters received.

The error factory can receive:

  • A string
  • An error instance
  • An error type: this param defines the error type returned

The errors returned are defined as the error type received.

  • ElasticError
  • WazuhApiError
  • UIError
  • etc

Error handler

The error handler is responsible to receive the errors (or strings) and define what type of error will be returned. After identifying and classifying the parameters received the error factory returns a new error instance. Always will return an error instance.

React patterns

The error handler can be implemented using react patterns:

  • HOC (Higher order component)

  • Hook

  • Decorator (Coming soon)

Machi3mfl avatar Feb 01 '23 15:02 Machi3mfl

How to use Error handler

Exists 3 ways to implement the error handler

  • using javascript class errorHandler
  • use a react hook called useErrorHanlder
  • use a react HOC called withErrorHandler

These types of error handlers were created to give flexibility to the error management implementation. All these implementations encapsulate the error handler class.

Error management architecture

graph TD;
 withErrorHandler-HOC-->ErrorHandler
 useErrorHandler-Hook-->ErrorHandler
 ErrorHandler-->ErrorFactory
 ErrorFactory-->WazuhApiError
 ErrorFactory-->WazuhReportingError
 ErrorFactory-->ElasticApiError
 ErrorFactory-->ElasticError
 ErrorFactory-->Etc

How to use Error Handler (class)

The recommended use of the Error Handler is in javascript methods (not react component). This handler will receive an Error instance or error message and it will classify and categorize the error by its structure and create and return the corresponding Error instance.

Example

import ErrorHandler from 'error-handler';

// the handlerError is a static method
const newErrorCreated = ErrorHandler.handleError(errorResponse);
// the newErrorCreated var could be anyone error type defined in the graph above

How to use Error Handler (Hook)

The recommended use of the Error handler hook is when we have any method inside a component that makes an API call that can fail. In this case, will pass the async method like a javascript callback.

Example


import { useErrorHandler } from 'useErrorHandler'

const anyAsyncFunction = async () => {
      // this method could return an error or not
};

const [res, error] = useErrorHandler(anyAsyncFunction);

if(error){
   // treat the error
}
  
// the res var store the method response (API response)

Important In this way, using the useErrorHandler hook we can omit the use of try-catch and do the code clear.

How to use Error Handler (HOC)

The recommended use of the Error Handler HOC is to catch all the errors produced in the component lifecycle. This HOC will wrap the react component and will catch all the errors and treat them by the error handler class

Example


import { withErrorHandler } from 'withErrorHandler'

const Component = (props) => {
      useEffect(() => {
        // Component did mount
        functionWithError();
      }, []);
      return <div>Example Component</div>;
};

const ComponentWrapped = withErrorHandler(Component);

In this way, using the errorHandler HOC we can catch all the errors by the error handler class

How to use Error Handler (Decorator) coming soon

Machi3mfl avatar Feb 02 '23 19:02 Machi3mfl

Errors Classes

The next diagram shows how is the relationship between the different types of errors created.

classDiagram

class iWazuhError {
    <<interface>>
    +Error error
    +string message
    +integer code    
    +handle()
}

iWazuhError <|-- WazuhError : implements
WazuhError <|-- WazuhApiError : extends
WazuhError <|-- WazuhReportingError : extends
WazuhError <|-- ElasticApiError : extends
WazuhError <|-- ElasticError : extends

Machi3mfl avatar Feb 06 '23 12:02 Machi3mfl

Unit tests (Updated 17/02/23)

  • Error factory
  • Error handler
  • Error handler HOC
  • Error handler Hook

Screenshot 2023-02-17 at 15 40 05

Machi3mfl avatar Feb 14 '23 16:02 Machi3mfl

Tasks

  • [x] Implement ErrorHandler in plugins/wazuh-kibana-app/public/components/common/error-boundary

Machi3mfl avatar Feb 17 '23 14:02 Machi3mfl

thought: Personally, I don't like the usage of the ErrorOrchestrator service. It looks overcomplicated to me.

Desvelao avatar Feb 22 '23 11:02 Desvelao

praise: Good job adding some documentation about error management.

Desvelao avatar Feb 22 '23 11:02 Desvelao

thought: I feel we should discuss how we want to manage the errors and the required implementation.

Desvelao avatar Feb 22 '23 11:02 Desvelao

thought: Personally, I don't like the usage of the ErrorOrchestrator service. It looks overcomplicated to me.

I think is better to use the existing ErrorOrchestrator to avoid rewriting a new Error Logger. But, we can prepare the implementation to change the ErrorOrchestrator after with the lowest possible cost.

Machi3mfl avatar Feb 22 '23 12:02 Machi3mfl

Next steps

  • [x] Add uses cases and document uses (In progress)
    • Added docs folders with use cases README and unit test for every use case
  • [x] See how to improve the error categorization - change the categorization by error code
    • Change the method of recognizing type errors for error URL requests inside error details inside code error received in an error message from the server-side plugin
  • [x] Add documentation about how the error is shown in the UI
  • [x] Get an issue related to wrong error handling and apply the new error management solution (In progress)
    • Issue to solve: #5210

Machi3mfl avatar Feb 27 '23 15:02 Machi3mfl

Branch updated with rebase.

AlexRuiz7 avatar Mar 09 '23 12:03 AlexRuiz7

Code coverage (Jest) % values
Statements 9.45% ( 3500 / 37028 )
Branches 5% ( 1436 / 28712 )
Functions 8.11% ( 745 / 9175 )
Lines 9.53% ( 3380 / 35459 )

github-actions[bot] avatar Apr 17 '23 13:04 github-actions[bot]