-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
error.ts
47 lines (43 loc) · 1.11 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Internal dependencies
*/
import { isDevelopmentMode } from './utils';
export const logError = (
error: Record< string, string > & { message: string }
): void => {
const onLoggingError = ( e: unknown ) => {
if ( isDevelopmentMode ) {
console.error( '[ExPlat] Unable to send error to server:', e ); // eslint-disable-line no-console
}
};
try {
const { message, ...properties } = error;
const logStashError = {
message,
properties: {
...properties,
context: 'explat',
explat_client: 'woocommerce',
},
};
if ( isDevelopmentMode ) {
console.error( '[ExPlat] ', error.message, error ); // eslint-disable-line no-console
} else {
if ( ! window.wcTracks?.isEnabled && ! window?._wca?.push ) {
throw new Error(
`Tracking is disabled, can't send error to the server`
);
}
const body = new window.FormData();
body.append( 'error', JSON.stringify( logStashError ) );
window
.fetch( 'https://public-api.wordpress.com/rest/v1.1/js-error', {
method: 'POST',
body,
} )
.catch( onLoggingError );
}
} catch ( e ) {
onLoggingError( e );
}
};