I'm connected to a remote enter.js file and using a similar webpack config with webpack version 5. We define the file name as remote enter.js, which can be connected from our host application. We expose the source index, which exports the header, profile, footer, and neuromodal modules. If we trigger the component error button, the application crashes. To resolve this, we wrap the header component with an error boundary, which displays an alternative UI and notifies users. We do the same for the app.js file, using a container error boundary. By defining these error boundaries, we can catch and track errors using the new Relic API. We report the errors caused by the header component and the host application, passing metadata such as module name, module version, error type, component source, and user ID. Tracking errors with the API is a good practice, especially when there are issues with external APIs like the Rekordmort API.
I'm connected to this remote enter.js file, and I'm also able to use a similar webpack config with webpack version 5 using the module federation. I'm defining this file name, the remote enter.js, that we're able to connect from our host application, and expose the source index which is nothing more than a file that is exporting the header, profile, footer, and neuromodal modules.
So as you can see, if we go back to our application, we might have noticed that we have some other buttons that are called component error. So here, if I trigger this button, you might have noticed that my whole application has crashed. If I reload my app and try to do the same from my header and click on component error, the application is crashing again. The component error that is triggering an error inside the component from the header module is affecting the whole application and makes the whole application crash. And how we can resolve that? What we can actually do is, aside our header component, the header component is a simple header element defining a title and some buttons, and we can wrap it up with some boundary and export this as default. The error boundary is just another component, and we're passing down the name of the component that this error is happening. So if I open this error boundary, you can see that it's nothing more than a simple React component that is able to derive some state when error actually occurs and is setting the state that there is an error. Based on this, we can actually display an alternative UI and instead of the component itself and notify our users. We can do the same thing for our app.js and we can wrap the app.js with a container error boundary as here. In a similar way, and you can see that the container error boundary is a similar React component that derives some state, defines the state, and we're able to display an alternative UI when an error occurs.
Going back to our application now, if I refresh the app, you can see that now when I'm triggering a component error from the header module, this is not affecting any more the My Account host application, My Account Microfrontend. So we display an alternative UI, notifying us about an error in the Microfrontend module of header, and we're able to reset that, we can trigger again the component error without having the application to crash. We can do the similar thing in My Account host application. But in this case, the whole application will crash because this affects the whole application, the modules that we render inside this host app. So now that we have defined these error boundaries, how we can use them? We can actually when we catch this component, these errors in this component, we can track some errors. We are using the window, the new Relic API. We have we are loading new Relic in our index.html file and we're able to define and track some errors that are happening inside this component. So when we're up the header component with this modular boundary, when error happens inside this header component, we're able to catch this error inside this component. And that way, we're using the new Relic API that is called notice error to report the error that is caused from the header component and pass down some configuration, some metadata, which is going to be some module name and module version as you can see here at the bottom that we're loading from our package station. And some further information like an error type, which is error boundary. Some component source, which is a prop name that we pass from the component that we wrap this boundary with. And also the user ID, which is the user ID for our user in this session. We were able to do the same thing in the container error boundary. As you can see over here, we're actually reporting the same error, not the same error, but the error that's happening in the host application. And we are able to define another boundary and the errors that are happening inside the host application. So what else we can track. As we noticed previously, we use profile in order to fetch some characters from the Rekordmort API. But what happens when there is an issue with this API, we will need, and it's a really good practice to track this kind of errors.
Comments