|
1 |
| -Angular 1 |
| 1 | +Angular |
2 | 2 | =========
|
3 | 3 |
|
4 |
| -To use Sentry with your Angular 1.x application, you will need to use both Raven.js (Sentry's browser JavaScript SDK) and the Raven.js Angular plugin. |
| 4 | +This document uses Angular to refer to Angular 2+. 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>`. |
5 | 5 |
|
6 |
| -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>`. |
| 6 | +Additionally, Raven.js can be configured to catch any Angular-specific (2.x) exceptions reported through the `@angular/core/ErrorHandler |
| 7 | +<https://angular.io/docs/js/latest/api/core/index/ErrorHandler-class.html>`_ component. |
7 | 8 |
|
8 |
| -Additionally, the Raven.js Angular plugin will catch any Angular-specific exceptions reported through Angular's ``$exceptionHandler`` interface. |
9 | 9 |
|
10 |
| -**Note**: This documentation is for Angular 1.x. See also: :doc:`Angular 2.x <angular2>` |
| 10 | +TypeScript Support |
| 11 | +------------------ |
11 | 12 |
|
12 |
| -Installation |
13 |
| ------------- |
14 |
| - |
15 |
| -Raven.js and the Raven.js Angular plugin are distributed using a few different methods. |
| 13 | +Raven.js ships with a `TypeScript declaration file |
| 14 | +<https://github.com/getsentry/raven-js/blob/master/typescript/raven.d.ts>`_, which helps static checking correctness of |
| 15 | +Raven.js API calls, and facilitates auto-complete in TypeScript-aware IDEs like Visual Studio Code. |
16 | 16 |
|
17 |
| -Using our CDN |
18 |
| -~~~~~~~~~~~~~ |
19 | 17 |
|
20 |
| -For convenience, our CDN serves a single, minified JavaScript file containing both Raven.js and the Raven.js Angular plugin. It should be included **after** Angular, but **before** your application code. |
| 18 | +Installation |
| 19 | +------------ |
21 | 20 |
|
22 |
| -Example: |
| 21 | +Raven.js should be installed via npm. |
23 | 22 |
|
24 |
| -.. sourcecode:: html |
| 23 | +.. code-block:: sh |
25 | 24 |
|
26 |
| - <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> |
27 |
| - <script src="https://cdn.ravenjs.com/###RAVEN_VERSION###/angular/raven.min.js" crossorigin="anonymous"></script> |
28 |
| - <script>Raven.config('___PUBLIC_DSN___').install();</script> |
| 25 | + $ npm install raven-js --save |
29 | 26 |
|
30 |
| -Note that this CDN build auto-initializes the Angular plugin. |
31 | 27 |
|
32 |
| -Using package managers |
33 |
| -~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | +Configuration |
| 29 | +------------- |
34 | 30 |
|
35 |
| -Pre-built distributions of Raven.js and the Raven.js Angular plugin are available via both Bower and npm. |
| 31 | +Configuration depends on which module loader/packager you are using to build your Angular application. |
36 | 32 |
|
37 |
| -Bower |
38 |
| -````` |
| 33 | +Below are instructions for `SystemJS |
| 34 | +<https://github.com/systemjs/systemjs>`__, followed by instructions for `Webpack |
| 35 | +<https://webpack.github.io/>`__, Angular CLI, and other module loaders/packagers. |
39 | 36 |
|
40 |
| -.. code |
| 37 | +SystemJS |
| 38 | +~~~~~~~~ |
41 | 39 |
|
42 |
| -.. code-block:: sh |
| 40 | +First, configure SystemJS to locate the Raven.js package: |
43 | 41 |
|
44 |
| - $ bower install raven-js --save |
| 42 | +.. code-block:: js |
45 | 43 |
|
46 |
| -.. code-block:: html |
| 44 | + System.config({ |
| 45 | + packages: { |
| 46 | + /* ... existing packages above ... */ |
| 47 | + 'raven-js': { |
| 48 | + main: 'dist/raven.js' |
| 49 | + } |
| 50 | + }, |
| 51 | + paths: { |
| 52 | + /* ... existing paths above ... */ |
| 53 | + 'raven-js': 'node_modules/raven-js' |
| 54 | + } |
| 55 | + }); |
47 | 56 |
|
48 |
| - <script src="/bower_components/angular/angular.js"></script> |
49 |
| - <script src="/bower_components/raven-js/dist/raven.js"></script> |
50 |
| - <script src="/bower_components/raven-js/dist/plugins/angular.js"></script> |
51 |
| - <script> |
52 |
| - Raven |
53 |
| - .config('___PUBLIC_DSN___') |
54 |
| - .addPlugin(Raven.Plugins.Angular) |
55 |
| - .install(); |
56 |
| - </script> |
| 57 | +Then, in your main module file (where ``@NgModule`` is called, e.g. app.module.ts): |
57 | 58 |
|
58 |
| -npm |
59 |
| -```` |
| 59 | +.. code-block:: js |
60 | 60 |
|
61 |
| -.. code-block:: sh |
| 61 | + import Raven = require('raven-js'); |
| 62 | + import { BrowserModule } from '@angular/platform-browser'; |
| 63 | + import { NgModule, ErrorHandler } from '@angular/core'; |
| 64 | + import { AppComponent } from './app.component'; |
62 | 65 |
|
63 |
| - $ npm install raven-js --save |
64 |
| -
|
65 |
| -.. code-block:: html |
66 |
| - |
67 |
| - <script src="/node_modules/angular/angular.js"></script> |
68 |
| - <script src="/node_modules/raven-js/dist/raven.js"></script> |
69 |
| - <script src="/node_modules/raven-js/dist/plugins/angular.js"></script> |
70 |
| - <script> |
71 |
| - Raven |
72 |
| - .config('___PUBLIC_DSN___') |
73 |
| - .addPlugin(Raven.Plugins.Angular) |
74 |
| - .install(); |
75 |
| - </script> |
| 66 | + Raven |
| 67 | + .config('___PUBLIC_DSN___') |
| 68 | + .install(); |
76 | 69 |
|
77 |
| -These examples assume that Angular is exported globally as `window.angular`. You can alternatively pass a reference to the `angular` object directly as the second argument to `addPlugin`: |
| 70 | + export class RavenErrorHandler implements ErrorHandler { |
| 71 | + handleError(err:any) : void { |
| 72 | + Raven.captureException(err.originalError || err); |
| 73 | + } |
| 74 | + } |
78 | 75 |
|
79 |
| -.. code-block:: javascript |
| 76 | + @NgModule({ |
| 77 | + imports: [ BrowserModule ], |
| 78 | + declarations: [ AppComponent ], |
| 79 | + bootstrap: [ AppComponent ], |
| 80 | + providers: [ { provide: ErrorHandler, useClass: RavenErrorHandler } ] |
| 81 | + }) |
| 82 | + export class AppModule { } |
80 | 83 |
|
81 |
| - Raven.addPlugin(Raven.Plugins.Angular, angular); |
| 84 | +Once you've completed these two steps, you are done. |
82 | 85 |
|
83 |
| -Module loaders (CommonJS) |
84 |
| -~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 86 | +Angular CLI |
| 87 | +~~~~~~~~~~~ |
85 | 88 |
|
86 |
| -Raven and the Raven Angular plugin can be loaded using a module loader like Browserify or Webpack. |
| 89 | +Angular CLI now uses Webpack to build instead of SystemJS. All you need to do is modify your main module file (where ``@NgModule`` is called, e.g. app.module.ts): |
87 | 90 |
|
88 |
| -.. code-block:: javascript |
| 91 | +.. code-block:: js |
89 | 92 |
|
90 |
| - var angular = require('angular'); |
91 |
| - var Raven = require('raven-js'); |
| 93 | + import * as Raven from 'raven-js'; |
| 94 | + import { BrowserModule } from '@angular/platform-browser'; |
| 95 | + import { NgModule, ErrorHandler } from '@angular/core'; |
| 96 | + import { AppComponent } from './app.component'; |
92 | 97 |
|
93 | 98 | Raven
|
94 | 99 | .config('___PUBLIC_DSN___')
|
95 |
| - .addPlugin(require('raven-js/plugins/angular'), angular) |
96 | 100 | .install();
|
97 | 101 |
|
98 |
| -Note that when using CommonJS-style imports, you must pass a reference to `angular` as the second argument to `addPlugin`. |
99 |
| - |
100 |
| -Angular Configuration |
101 |
| ---------------------- |
102 |
| - |
103 |
| -Inside your main Angular application module, you need to declare `ngRaven` as a module dependency: |
104 |
| - |
105 |
| -.. code-block:: javascript |
106 |
| -
|
107 |
| - var myApp = angular.module('myApp', [ |
108 |
| - 'ngRaven', |
109 |
| - 'ngRoute', |
110 |
| - 'myAppControllers', |
111 |
| - 'myAppFilters' |
112 |
| - ]); |
| 102 | + export class RavenErrorHandler implements ErrorHandler { |
| 103 | + handleError(err:any) : void { |
| 104 | + Raven.captureException(err.originalError); |
| 105 | + } |
| 106 | + } |
| 107 | +
|
| 108 | + @NgModule({ |
| 109 | + imports: [ BrowserModule ], |
| 110 | + declarations: [ AppComponent ], |
| 111 | + bootstrap: [ AppComponent ], |
| 112 | + providers: [ { provide: ErrorHandler, useClass: RavenErrorHandler } ] |
| 113 | + }) |
| 114 | + export class AppModule { } |
| 115 | +
|
| 116 | +Once you've completed that step, you are done. |
0 commit comments