Skip to content

Commit

Permalink
add ui_errors to clientStats
Browse files Browse the repository at this point in the history
Whenever the user sees an error, it will be recorded in clientStats as a `stats/client_error`
  • Loading branch information
JGreenlee committed Sep 13, 2024
1 parent 690a516 commit f8fcf10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
8 changes: 6 additions & 2 deletions www/__tests__/clientStats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ it('stores a client stats event', async () => {

it('stores a client stats error', async () => {
const errorStr = 'test error';
await addStatError('missing_keys', errorStr);
try {
throw new Error(errorStr);
} catch (error) {
await addStatError(error.message);
}
const storedMessages = await db.getAllMessages('stats/client_error', false);
expect(storedMessages).toContainEqual({
name: 'missing_keys',
name: 'ui_error',
ts: expect.any(Number),
reading: errorStr,
client_app_version: '1.2.3',
Expand Down
7 changes: 4 additions & 3 deletions www/js/plugin/clientStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type StatKey =
| 'set_reminder_prefs'
| 'force_sync'
| 'open_notification'
| 'missing_keys';
| 'missing_keys'
| 'ui_error';

let appVersion;
export function getAppVersion() {
Expand All @@ -39,9 +40,9 @@ export async function addStatReading(name: StatKey, reading?: any) {
displayErrorMsg('addStatReading: db is not defined');
}

export async function addStatError(name: StatKey, errorStr: string) {
export async function addStatError(errorMsg: string) {
const db = window['cordova']?.plugins?.BEMUserCache;
const event = await getStatsEvent(name, errorStr);
const event = await getStatsEvent('ui_error', errorMsg);
logDebug('addStatError: adding CLIENT_ERROR event: ' + JSON.stringify(event));
if (db) return db.putMessage(CLIENT_ERROR, event);
displayErrorMsg('addStatError: db is not defined');
Expand Down
3 changes: 3 additions & 0 deletions www/js/plugin/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { addStatError } from './clientStats';

export const logDebug = (message: string) =>
window['Logger']?.log(window['Logger'].LEVEL_DEBUG, message);

Expand All @@ -19,6 +21,7 @@ export function displayErrorMsg(errorMsg: string, title?: string) {
}
const displayMsg = `━━━━\n${title}\n━━━━\n` + errorMsg;
window.alert(displayMsg);
addStatError(title ? `${title}: ${errorMsg}` : errorMsg);
console.error(displayMsg);
window['Logger']?.log(window['Logger'].LEVEL_ERROR, displayMsg);
}

0 comments on commit f8fcf10

Please sign in to comment.