-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposing some changes that I mentioned [here](#236 (comment)). To recap, I think the error logging in Stimulus is great, but several people (me included) have been caught by the fact that errors aren't reported elsewhere out of the box. The aim of this PR is to automatically integrate with third party error tracking services where possible, and to improve the documentation where not. Specific changes: - If `window.onerror` is defined, Stimulus will now call it after logging an error. Many error tracking tools define this method, eg. [Sentry](https://github.com/getsentry/sentry-javascript/blob/0ee07995d415d3870608c477cbdcf8445a51e1bb/packages/browser/src/loader.js#L192), [Airbrake](https://github.com/airbrake/airbrake-js/blob/9d4787b1c559aa39107d7288f46c4108c9a9d954/packages/browser/src/notifier.ts#L70) - Added documentation on how error handling works, including what happens out of the box and how to override it (with code sample from #53) - Added tests for the error handler.
- Loading branch information
1 parent
c47f551
commit e37a2be
Showing
5 changed files
with
87 additions
and
1 deletion.
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
59 changes: 59 additions & 0 deletions
59
packages/@stimulus/core/src/tests/modules/error_handler_tests.ts
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Controller } from "../.." | ||
import { ControllerTestCase } from "../cases/controller_test_case" | ||
import { TestApplicationWithDefaultErrorBehavior } from "../cases/application_test_case" | ||
|
||
class MockLogger { | ||
errors: any[] = [] | ||
logs: any[] = [] | ||
warns: any[] = [] | ||
|
||
log(event: any) { | ||
this.logs.push(event) | ||
} | ||
|
||
error(event: any) { | ||
this.errors.push(event) | ||
} | ||
|
||
warn(event: any) { | ||
this.warns.push(event) | ||
} | ||
} | ||
|
||
class ErrorWhileConnectingController extends Controller { | ||
connect() { | ||
throw new Error('bad!'); | ||
} | ||
} | ||
|
||
export default class ErrorHandlerTests extends ControllerTestCase(ErrorWhileConnectingController) { | ||
controllerConstructor = ErrorWhileConnectingController | ||
|
||
async setupApplication() { | ||
const logger = new MockLogger() | ||
|
||
this.application = new TestApplicationWithDefaultErrorBehavior(this.fixtureElement, this.schema) | ||
this.application.logger = logger | ||
|
||
window.onerror = function(message, source, lineno, colno, error) { | ||
logger.log(`error from window.onerror. message = ${message}, source = ${source}, lineno = ${lineno}, colno = ${colno}`) | ||
} | ||
|
||
await super.setupApplication() | ||
} | ||
|
||
async "test errors in connect are thrown and handled by built in logger"() { | ||
const mockLogger: any = this.application.logger | ||
|
||
// when `ErrorWhileConnectingController#connect` throws, the controller's application's logger's `error` function | ||
// is called; in this case that's `MockLogger#error`. | ||
this.assert.equal(1, mockLogger.errors.length) | ||
} | ||
|
||
async "test errors in connect are thrown and handled by window.onerror"() { | ||
const mockLogger: any = this.application.logger | ||
|
||
this.assert.equal(1, mockLogger.logs.length) | ||
this.assert.equal('error from window.onerror. message = Error connecting controller, source = , lineno = 0, colno = 0', mockLogger.logs[0]) | ||
} | ||
} |