-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Vue.js (2.0) plugin #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,65 @@ | ||
Vue.js (2.0) | ||
============ | ||
|
||
.. sentry:support-warning:: | ||
|
||
This plugin only works with Vue 2.0 or greater. | ||
|
||
|
||
To use Sentry with your Vue application, you will need to use both Raven.js (Sentry's browser JavaScript SDK) and the Raven.js Vue plugin. | ||
|
||
On its own, Raven.js will report any uncaught exceptions triggered from your application. For advanced usage examples of Raven.js, please read :doc:`Raven.js usage <../usage>`. | ||
|
||
Additionally, the Raven.js Vue plugin will capture the name and props state of the active component where the error was thrown. This is reported via Vue's `config.errorHandler` hook. | ||
|
||
Installation | ||
------------ | ||
|
||
Raven.js and the Raven.js Vue plugin are distributed using a few different methods. | ||
|
||
Using our CDN | ||
~~~~~~~~~~~~~ | ||
|
||
For convenience, our CDN serves a single, minified JavaScript file containing both Raven.js and the Raven.js Vue plugin. It should be included **after** Vue, but **before** your application code. | ||
|
||
Example: | ||
|
||
.. sourcecode:: html | ||
|
||
<script src="http://builds.emberjs.com/tags/v2.3.1/ember.prod.js"></script> | ||
<script src="https://cdn.ravenjs.com/3.4.1/vue/raven.min.js"></script> | ||
<script>Raven.config('___PUBLIC_DSN___').install();</script> | ||
|
||
Note that this CDN build auto-initializes the Vue plugin. | ||
|
||
Using package managers | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Both Raven.js and the Raven.js Vue plugin can be installed via npm and Bower. | ||
|
||
npm | ||
```` | ||
|
||
.. code-block:: sh | ||
|
||
$ npm install raven-js --save | ||
|
||
|
||
Bower | ||
````` | ||
|
||
.. code-block:: sh | ||
|
||
$ bower install raven-js --save | ||
|
||
In your main application file, import and configure both Raven.js and the Raven.js Vue plugin as follows: | ||
|
||
.. code-block:: js | ||
|
||
import Raven from 'raven-js'; | ||
import RavenVue from 'raven-js/plugins/vue'; | ||
|
||
Raven | ||
.config('___PUBLIC_DSN___') | ||
.addPlugin(RavenVue) | ||
.install(); |
This file contains hidden or 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,29 @@ | ||
/** | ||
* Vue.js 2.0 plugin | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
function vuePlugin(Raven, Vue) { | ||
Vue = Vue || window.Vue; | ||
|
||
// quit if Vue isn't on the page | ||
if (!Vue || !Vue.config) return; | ||
|
||
var _oldOnError = Vue.config.errorHandler; | ||
Vue.config.errorHandler = function VueErrorHandler(error, vm) { | ||
Raven.captureException(error, { | ||
extra: { | ||
componentName: Vue.util.formatComponentName(vm), | ||
propsData: vm.$options.propsData | ||
} | ||
}); | ||
|
||
if (typeof _oldOnError === 'function') { | ||
_oldOnError.call(this, error, vm); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = vuePlugin; | ||
|
This file contains hidden or 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,62 @@ | ||
var _Raven = require('../../src/raven'); | ||
var vuePlugin = require('../../plugins/vue'); | ||
|
||
var Raven; | ||
describe('Vue plugin', function () { | ||
beforeEach(function () { | ||
Raven = new _Raven(); | ||
Raven.config('http://[email protected]:80/2'); | ||
}); | ||
|
||
describe('errorHandler', function () { | ||
beforeEach(function () { | ||
this.sinon.stub(Raven, 'captureException'); | ||
this.MockVue = { | ||
config: {}, | ||
util: { | ||
formatComponentName: function() { | ||
return '<root component>' | ||
} | ||
} | ||
}; | ||
}); | ||
|
||
it('should capture component name and propsData', function () { | ||
vuePlugin(Raven, this.MockVue); | ||
|
||
this.MockVue.config.errorHandler(new Error('foo'), { | ||
$options: { | ||
propsData: { | ||
foo: 'bar' | ||
} | ||
} | ||
}, {} /* vm */); | ||
|
||
assert.isTrue(Raven.captureException.calledOnce); | ||
|
||
assert.deepEqual(Raven.captureException.args[0][1].extra, { | ||
propsData: { | ||
foo: 'bar' | ||
}, | ||
componentName: '<root component>' | ||
}); | ||
}); | ||
|
||
it('should call the existing error handler', function () { | ||
var errorHandler = this.sinon.stub(); | ||
this.MockVue.config.errorHandler = errorHandler; | ||
vuePlugin(Raven, this.MockVue); // should override errorHandler | ||
|
||
var err = new Error('foo'); | ||
var vm = { | ||
$options: { propsData: {} } | ||
}; | ||
this.MockVue.config.errorHandler(err, vm); | ||
|
||
assert.isTrue(Raven.captureException.calledOnce); | ||
assert.isTrue(errorHandler.calledOnce); | ||
assert.equal(errorHandler.args[0][0], err); | ||
assert.equal(errorHandler.args[0][1], vm); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops