-1

I'm using Azure app Insights for collecting analytical data.I want to handle a case where I'm getting a 400 error due to providing the wrong instrumentation key in the AppInsights.js file. I have created a profile for encrypting instrumentation key in App. Where can I catch this error in code and display fallback UI in a React App.The backend is in Java.

Errors

import { ApplicationInsights from'@microsoft/applicationinsightsweb';
import {ReactPlugin,AppInsightsErrorBoundary,} from 
'@microsoft/applicationinsights-react-js';
import { createBrowserHistory } from 'history';
import { ClickAnalyticsPlugin } from '@microsoft/applicationinsights- 
clickanalytics-js';

import io from 'react/IOGlobal';
const clickPluginInstance = new ClickAnalyticsPlugin();

const browserHistory = createBrowserHistory({ basename: '' });
const reactPlugin = new ReactPlugin();
const clickPluginConfig = {
  autoCapture: true,
  dataTags: {
    useDefaultContentNameOrId: true,
  },
};
const appInsights = new ApplicationInsights({
  config: { instrumentationKey: 
***io.platform.getProfileValue('APP_INSTRUMENTATION_KEY')***,
extensions: [reactPlugin, clickPluginInstance],
extensionConfig: {
  [reactPlugin.identifier]: { history: browserHistory },
  [clickPluginInstance.identifier]: clickPluginConfig,
    },
  },
});

appInsights.loadAppInsights();
export default { reactPlugin, appInsights };

Also please help with any other implementation in App Insights to catch the same error. Thanks In Advance.

  • Hi and welcome to Stack Overflow. In its current form, this question is not a great fit for SO. It is not clear what you actually want to achieve and where you failed. Please refer to [ask]. On-topic: are you trying to show something like a message-box when the `track` requests error out because of misconfiguration? – rickvdbosch Oct 21 '21 at 07:51
  • you are right @rickvdboch. I want to show an error message when a track request gets failed. I have created a profile for encrypting instrumentation key in App , But when we provide the wrong value for the same, an error message should be displayed. – Pavan Karnalkar Oct 21 '21 at 09:20

1 Answers1

0

Update index.js by putting the following route at the end of the route declaration, before app.listen():

…

// this matches all routes and all methods

app.use((req, res, next) => {
    res.status(404).send({
    status: 404,
    error: ‘Not found’
    // You can add your alert
})
})

Check here for more info here

First need to set the Instrumentation key .setup(“Instrumentation key”) To get the instance of the default client that is configured with .setup("Instrumentation key") use appInsights.client. Alternatively, if you want a new client just use getClient("Instrumentation key").

Just like for setup, you can skip providing the Instrumentation key in code if it's in the special instrumentation key environment variable.

Refer for similar kind of issue here

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15