Skip to content

Commit 277d54c

Browse files
committed
feat: AngularJS integration
1 parent 57f8e3a commit 277d54c

File tree

6 files changed

+112
-17
lines changed

6 files changed

+112
-17
lines changed

packages/integrations/package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"tslib": "^1.9.3"
2222
},
2323
"devDependencies": {
24+
"@types/angular": "^1.6.54",
2425
"chai": "^4.1.2",
2526
"jest": "^24.1.0",
2627
"npm-run-all": "^4.1.2",
@@ -31,11 +32,7 @@
3132
"typescript": "^3.3.3"
3233
},
3334
"scripts": {
34-
"build": "run-s clean && run-p build:es5 build:esm",
35-
"build:es5": "tsc -p tsconfig.build.json",
36-
"build:esm": "run-s build:esm:transpile build:esm:rewrite",
37-
"build:esm:transpile": "tsc -p tsconfig.esm.json",
38-
"build:esm:rewrite": "node ../../scripts/esm-rewrite.js",
35+
"build": "tsc -p tsconfig.build.json",
3936
"build:watch": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
4037
"clean": "rimraf dist coverage *.js *.js.map *.d.ts",
4138
"lint": "run-s lint:prettier lint:tslint",

packages/integrations/src/angular.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { captureException, getCurrentHub, withScope } from '@sentry/core';
2+
import { Event, Integration } from '@sentry/types';
3+
import { logger } from '@sentry/utils/logger';
4+
import { getGlobalObject } from '@sentry/utils/misc';
5+
6+
// See https://github.com/angular/angular.js/blob/v1.4.7/src/minErr.js
7+
const angularPattern = /^\[((?:[$a-zA-Z0-9]+:)?(?:[$a-zA-Z0-9]+))\] (.*?)\n?(\S+)$/;
8+
9+
/**
10+
* AngularJS integration
11+
*
12+
* Provides an $exceptionHandler for AngularJS
13+
*/
14+
export class Angular implements Integration {
15+
/**
16+
* @inheritDoc
17+
*/
18+
public name: string = Angular.id;
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public static id: string = 'AngularJS';
24+
25+
/**
26+
* moduleName used in Angular's DI resolution algorithm
27+
*/
28+
public static moduleName: string = 'ngSentry';
29+
30+
/**
31+
* Angular's instance
32+
*/
33+
private readonly angular: ng.IAngularStatic;
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public constructor(options: { angular?: ng.IAngularStatic } = {}) {
39+
this.angular =
40+
options.angular ||
41+
(getGlobalObject() as {
42+
angular: ng.IAngularStatic;
43+
}).angular;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public setupOnce(): void {
50+
if (!this.angular) {
51+
logger.error('AngularIntegration is missing an Angular instance');
52+
return;
53+
}
54+
55+
this.angular.module(Angular.moduleName, []).config([
56+
'$provide',
57+
($provide: ng.auto.IProvideService) => {
58+
$provide.decorator('$exceptionHandler', ['$delegate', this.$exceptionHandlerDecorator.bind(this)]);
59+
},
60+
]);
61+
}
62+
63+
/**
64+
* Angular's exceptionHandler for Sentry integration
65+
*/
66+
private $exceptionHandlerDecorator($delegate: ng.IExceptionHandlerService): ng.IExceptionHandlerService {
67+
return (exception: Error, cause?: string) => {
68+
if (getCurrentHub().getIntegration(Angular)) {
69+
withScope(scope => {
70+
if (cause) {
71+
scope.setExtra('cause', cause);
72+
}
73+
74+
scope.addEventProcessor((event: Event) => {
75+
const ex = event.exception && event.exception.values && event.exception.values[0];
76+
77+
if (ex) {
78+
const matches = angularPattern.exec(ex.value || '');
79+
80+
if (matches) {
81+
// This type now becomes something like: $rootScope:inprog
82+
ex.type = matches[1];
83+
ex.value = matches[2];
84+
event.message = `${ex.type}: ${ex.value}`;
85+
// auto set a new tag specifically for the angular error url
86+
event.extra = {
87+
...event.extra,
88+
angularDocs: matches[3].substr(0, 250),
89+
};
90+
}
91+
}
92+
93+
return event;
94+
});
95+
96+
captureException(exception);
97+
});
98+
}
99+
$delegate(exception, cause);
100+
};
101+
}
102+
}

packages/integrations/tsconfig.build.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"baseUrl": ".",
55
"outDir": ".",
6-
"rootDir": "src"
6+
"rootDir": "src",
7+
"types": ["angular"]
78
},
89
"exclude": [],
910
"include": ["src/**/*.ts"]

packages/integrations/tsconfig.esm.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/integrations/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"exclude": ["dist"],
55
"compilerOptions": {
66
"rootDir": ".",
7-
"types": ["node", "jest"]
7+
"types": ["node", "jest", "angular"]
88
}
99
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,11 @@
895895
array-from "^2.1.1"
896896
lodash "^4.17.11"
897897

898+
"@types/angular@^1.6.54":
899+
version "1.6.54"
900+
resolved "https://registry.yarnpkg.com/@types/angular/-/angular-1.6.54.tgz#f9d5a03e4da7b021a6dabe5d63e899ed4567a5bd"
901+
integrity sha512-xA1FuozWXeRQ7FClUbvk8ePL+dydBeDoCWRPFTHU5+8uvVtIIfLGiHA8CMkwsbddFCYnTDVbLxG85a/HBx7LtA==
902+
898903
"@types/caseless@*":
899904
version "0.12.1"
900905
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.1.tgz#9794c69c8385d0192acc471a540d1f8e0d16218a"

0 commit comments

Comments
 (0)