Skip to content

Conversation

@javan
Copy link
Contributor

@javan javan commented Jan 10, 2018

Supply your own error handler function to override the default console.error. Useful for sending errors to a tracking service like Sentry.

const application = Application.start()

application.handleError = (error, message, detail) => {
  console.warn(message, detail)
  Raven.captureException(error)
}

Supply your own error handler function to override the default `console.error`.
@sstephenson sstephenson changed the base branch from master to 1-0 January 10, 2018 14:39
@sstephenson sstephenson added this to the 1.0 milestone Jan 10, 2018
@sstephenson sstephenson changed the title Add Application#setErrorHandler Add Application.prototype.handleError Jan 10, 2018
@sstephenson sstephenson merged commit 9690ffa into 1-0 Jan 10, 2018
@sstephenson sstephenson deleted the error-handler branch January 10, 2018 21:21
@rwojsznis
Copy link

Welp, that caught me by surprise (obviously in production 😅 )

I would never-ever except such library to swallow errors for me - maybe I ask why throw is not the default here? (so if you have sentry it will be reported properly via window.onerror by default)

And ofc thanks for this PR @javan ! ❤️

@sstephenson
Copy link
Contributor

@emq Could you open a new issue so your comment doesn’t get lost in the stream? Thank you!

ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Jan 8, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
ghiculescu added a commit to ghiculescu/stimulus that referenced this pull request Mar 25, 2021
Proposing some changes that I mentioned [here](hotwired#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 hotwired#53)
- Added tests for the error handler.
@formigarafa
Copy link

It seems the instructions provided in the docs to customize the handleError doesn't work.
The controllers still references the original handleError not the substituted/assigned one.

To solve I had to do it like this:

import { Application } from "@hotwired/stimulus"

const handleErrorOriginal = Application.prototype.handleError
Application.prototype.handleError = function(error, message, detail) {
  console.log("now custom error handler is called");
  handleErrorOriginal.call(this, error, message, detail);
}

const application = Application.start()

JayeTurn added a commit to JayeTurn/stimulus that referenced this pull request Jun 10, 2025
Proposing some changes that I mentioned [here](hotwired/stimulus#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 hotwired/stimulus#53)
- Added tests for the error handler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants