Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lejard-h committed Feb 20, 2018
0 parents commit e31f602
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Files and directories created by pub
.packages
.pub/
build/
# Remove the following pattern if you wish to check in your lock file
pubspec.lock

# Directory created by dartdoc
doc/api/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## 0.0.1

- Initial version, created by Stagehand
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Lefty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# angular_sentry

Helper to implements sentry with Angular.

## Usage

### Basic

```dart
import "package:angular/angular.dart";
import "package:angular_sentry/angular_sentry.dart";
main() {
bootstrap(MyApp, [
provide(SENTRY_DSN, useValue: "MY_SENTRY_DSN"),
provide(ExceptionHandler, useClass: AngularSentry)
]);
}
```

### Advanced

```dart
main() {
bootstrap(MyApp, [
AppSentry
]);
}
@Injectable()
class AppSentry extends AngularSentry {
AppSentry(Injector injector)
: super(injector, "MY_SENTRY_DSN");
SentryUser get user => new SentryUser();
String get environment => "production";
String get release => "1.0.0";
Map<String, String> get extra => {"location_url": window.location.href};
void onCatch(dynamic exception, Trace trace, [String reason]) {
if (exception is ClientException) {
log("Network error");
} else {
super.onCatch(exception, trace, reason);
}
}
}
```
15 changes: 15 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
analyzer:
strong-mode: true
# exclude:
# - path/to/excluded/files/**

# Lint rules and documentation, see http://dart-lang.github.io/linter/lints
linter:
rules:
- cancel_subscriptions
- hash_and_equals
- iterable_contains_unrelated_type
- list_remove_unrelated_type
- test_types_in_equals
- unrelated_type_equality_checks
- valid_regexps
134 changes: 134 additions & 0 deletions lib/angular_sentry.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
library angular_sentry;

import 'dart:async';

import 'package:angular/angular.dart';
import 'package:logging/logging.dart';
import 'package:sentry_client/api_data/sentry_exception.dart';
import 'package:sentry_client/api_data/sentry_packet.dart';
import 'package:sentry_client/api_data/sentry_stacktrace.dart';
import 'package:sentry_client/api_data/sentry_stacktrace_frame.dart';
import 'package:sentry_client/api_data/sentry_user.dart';
import 'package:sentry_client/sentry_client_browser.dart';
import 'package:sentry_client/sentry_dsn.dart';
import 'package:stack_trace/stack_trace.dart';

export 'package:sentry_client/api_data/sentry_user.dart';
export 'package:stack_trace/stack_trace.dart';

const OpaqueToken SENTRY_DSN = const OpaqueToken('sentryDSN');

@Injectable()
class AngularSentry implements ExceptionHandler {
SentryClientBrowser _sentry;
Logger _log;
Logger get log => _log;
ApplicationRef _appRef;

AngularSentry(
Injector injector, @Optional() @Inject(SENTRY_DSN) String sentryDSN) {
if (sentryDSN != null) {
_sentry = new SentryClientBrowser(
SentryDsn.fromString(sentryDSN, allowSecretKey: true));
}

_log = new Logger("$runtimeType");
// prevent DI circular dependency
new Future<Null>.delayed(Duration.ZERO, () {
_appRef = injector.get(ApplicationRef) as ApplicationRef;
});
}

void onCatch(dynamic exception, Trace trace, [String reason]) {
try {
_send(exception, trace);
} catch (e, s) {
logError(e, s);
// do nothing;
}
}

/// Log the catched error using Logging
/// Called before onCatch
void logError(dynamic exception, Trace trace, [String reason]) {
_log.severe(ExceptionHandler.exceptionToString(
exception,
trace,
reason,
));
}

@override
void call(dynamic exception, [dynamic stackTrace, String reason]) {
final trace = parseStackTrace(stackTrace);
logError(exception, trace, reason);
onCatch(exception, trace, reason);
_appRef?.tick();
}

/// provide environment data to the sentry report
String get environment => null;

/// provide user data to the sentry report
SentryUser get user => null;

/// The release version of the application.
String get release => null;

/// provide extra data to the sentry report
Map<String, String> get extra => null;

void _send(dynamic exception, Trace trace) {
final stacktraceValue = new SentryStacktrace(
frames: trace?.frames
?.map((Frame frame) => new SentryStacktraceFrame(
filename: frame?.uri?.toString(),
package: frame?.package,
lineno: frame?.line,
colno: frame?.column,
function: frame?.member,
module: frame?.library))
?.toList());

final exceptionValue = new SentryException(
type: exception?.runtimeType?.toString(),
value: exception?.toString(),
stacktrace: stacktraceValue);

final packet = new SentryPacket(
user: user,
environment: environment,
release: release,
exceptionValues: [exceptionValue],
extra: extra);
_sentry?.write(packet);
}
}

Trace parseStackTrace(dynamic stackTrace) {
if (stackTrace is StackTrace) {
return new Trace.parse(stackTrace.toString());
} else if (stackTrace is String) {
return new Trace.parse(stackTrace);
} else if (stackTrace is Iterable<Frame>) {
return new Trace(stackTrace);
} else if (stackTrace is Iterable<String>) {
return new Trace(stackTrace.map(parseFrame).where((f) => f != null));
}
return new Trace.current();
}

Frame parseFrame(String f) {
Frame parsed = new Frame.parseV8(f);
if (parsed is UnparsedFrame) {
parsed = new Frame.parseFirefox(f);
if (parsed is UnparsedFrame) {
try {
return new Frame.parseFriendly(f);
} catch (_) {
parsed = null;
}
}
}
return null;
}
17 changes: 17 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: angular_sentry
description: Helper to implements sentry with Angular
version: 0.0.1
homepage: https://lefty.io
author: Hadrien Lejard <hadrien.lejard@gmail.com>

environment:
sdk: '>=1.20.1 <2.0.0'

dependencies:
angular: ^4.0.0
sentry_client: ^4.0.0
stack_trace: ^1.9.0
logging: ^0.11.0

#dev_dependencies:
# test: ^0.12.0

0 comments on commit e31f602

Please sign in to comment.