-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
49 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,58 @@ | ||
import { reportError } from 'utils/report-error'; | ||
import { _, catchErrorsWithContext } from './robust'; | ||
import { catchErrorsAndReport } from './robust'; | ||
import type { Modules } from './robust'; | ||
|
||
const { catchAndLogError } = _; | ||
|
||
jest.mock('utils/report-error', () => ({ | ||
reportError: jest.fn(), | ||
})); | ||
|
||
let origConsoleWarn: typeof window.console.warn; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
origConsoleWarn = window.console.warn; | ||
window.console.warn = jest.fn(); | ||
jest.spyOn(global.console, 'warn'); | ||
}); | ||
|
||
afterEach(() => { | ||
window.console.warn = origConsoleWarn; | ||
jest.spyOn(global.console, 'warn').mockRestore(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('robust', () => { | ||
const ERROR = new Error('Something broke.'); | ||
const META = 'commercial'; | ||
const TAGS = { | ||
module: 'test', | ||
}; | ||
|
||
const noError = () => true; | ||
const ERROR = new Error('Deliberate test error'); | ||
const tags = { tag: 'test' }; | ||
|
||
const throwError = () => { | ||
throw ERROR; | ||
}; | ||
|
||
test('catchAndLogError()', () => { | ||
expect(() => { | ||
catchAndLogError('test', noError); | ||
}).not.toThrowError(); | ||
|
||
expect(() => { | ||
catchAndLogError('test', throwError); | ||
}).not.toThrowError(ERROR); | ||
test('catchErrorsAndReport with no errors', () => { | ||
const runner = jest.fn(); | ||
|
||
expect(window.console.warn).toHaveBeenCalledTimes(1); | ||
}); | ||
const modules = [ | ||
['test-1', runner], | ||
['test-2', runner], | ||
['test-3', runner], | ||
] as Modules; | ||
|
||
test('catchAndLogError() - default reporter with no error', () => { | ||
catchAndLogError('test', noError); | ||
catchErrorsAndReport(modules); | ||
expect(runner).toHaveBeenCalledTimes(modules.length); | ||
expect(reportError).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('catchAndLogError() - default reporter with error', () => { | ||
catchAndLogError('test', throwError); | ||
expect(reportError).toHaveBeenCalledWith(ERROR, META, TAGS); | ||
}); | ||
|
||
test('catchErrorsWithContext()', () => { | ||
test('catchErrorsAndReport with one error', () => { | ||
const runner = jest.fn(); | ||
|
||
const MODULES = [ | ||
const modules = [ | ||
['test-1', runner], | ||
['test-2', runner], | ||
['test-2', throwError], | ||
['test-3', runner], | ||
] as Modules; | ||
|
||
catchErrorsWithContext(MODULES); | ||
expect(runner).toHaveBeenCalledTimes(MODULES.length); | ||
catchErrorsAndReport(modules, tags); | ||
expect(runner).toHaveBeenCalledTimes(2); | ||
expect(reportError).toHaveBeenCalledTimes(1); | ||
expect(window.console.warn).toHaveBeenCalledTimes(1); | ||
expect(reportError).toHaveBeenCalledWith(ERROR, 'commercial', { | ||
tag: 'test', | ||
module: 'test-2', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,31 @@ | ||
/* | ||
Swallows (and reports) exceptions. Designed to wrap around modules at the "bootstrap" level. | ||
For example "comments throwing an exception should not stop auto refresh" | ||
*/ | ||
|
||
/** | ||
* Swallows and reports exceptions. Designed to wrap around modules at the "bootstrap" level. | ||
*/ | ||
import { reportError } from 'utils/report-error'; | ||
|
||
type ModuleFunction = () => void; | ||
type Module = [string, ModuleFunction]; | ||
type Modules = Module[]; | ||
|
||
const catchErrors = (fn: ModuleFunction): Error | undefined => { | ||
let error: Error | undefined; | ||
|
||
try { | ||
fn(); | ||
} catch (e) { | ||
error = e instanceof Error ? e : new Error(String(e)); | ||
} | ||
|
||
return error; | ||
}; | ||
|
||
const logError = ( | ||
moduleName: string, | ||
error: Error, | ||
tags?: Record<string, string>, | ||
): void => { | ||
window.console.warn('Caught error.', error.stack); | ||
reportError(error, 'commercial', { ...tags, module: moduleName }); | ||
}; | ||
|
||
const catchAndLogError = ( | ||
name: string, | ||
fn: ModuleFunction, | ||
tags?: Record<string, string>, | ||
): void => { | ||
const error = catchErrors(fn); | ||
|
||
if (error) { | ||
logError(name, error, tags); | ||
} | ||
}; | ||
|
||
const catchErrorsWithContext = ( | ||
const catchErrorsAndReport = ( | ||
modules: Modules, | ||
tags?: Record<string, string>, | ||
): void => { | ||
modules.forEach(([name, fn]) => catchAndLogError(name, fn, tags)); | ||
modules.forEach(([name, fn]) => { | ||
let error: Error | undefined; | ||
|
||
try { | ||
fn(); | ||
} catch (e) { | ||
error = e instanceof Error ? e : new Error(String(e)); | ||
} | ||
|
||
if (error) { | ||
window.console.warn('Caught error.', error.stack); | ||
reportError(error, 'commercial', { ...tags, module: name }); | ||
} | ||
}); | ||
}; | ||
|
||
export { catchErrorsWithContext }; | ||
export { catchErrorsAndReport }; | ||
export type { Modules }; | ||
export const _ = { catchAndLogError }; |