|
|
import wpcom from 'calypso/lib/wp'; |
|
|
import { getLogger } from 'calypso/server/lib/logger'; |
|
|
import { isDevelopmentMode } from './misc'; |
|
|
|
|
|
|
|
|
declare const window: undefined | ( Window & typeof globalThis ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const logError = ( error: Record< string, string > & { message: string } ): void => { |
|
|
const onError = ( e: unknown ) => { |
|
|
if ( isDevelopmentMode ) { |
|
|
|
|
|
console.error( '[ExPlat] Unable to send error to server:', e ); |
|
|
} |
|
|
}; |
|
|
|
|
|
try { |
|
|
const { message, ...properties } = error; |
|
|
const logStashError = { |
|
|
message, |
|
|
properties: { |
|
|
...properties, |
|
|
context: 'explat', |
|
|
explat_client: 'calypso', |
|
|
message, |
|
|
}, |
|
|
}; |
|
|
|
|
|
if ( typeof window === 'undefined' ) { |
|
|
|
|
|
getLogger().error( logStashError ); |
|
|
} else if ( isDevelopmentMode ) { |
|
|
|
|
|
console.error( '[ExPlat] ', error.message, error ); |
|
|
} else { |
|
|
wpcom.req.post( |
|
|
{ |
|
|
path: '/js-error', |
|
|
apiNamespace: 'rest/v1.1', |
|
|
body: { |
|
|
error: JSON.stringify( logStashError ), |
|
|
}, |
|
|
}, |
|
|
( err: unknown ) => err && onError( err ) |
|
|
); |
|
|
} |
|
|
} catch ( e ) { |
|
|
onError( e ); |
|
|
} |
|
|
}; |
|
|
|