Skip to content

Commit 9263eba

Browse files
authored
fix(cli): Sentry integration (#336)
Ensure Sentry integration handles all errors raised and respects NO_ANALYTICS setting.
1 parent 7f8a702 commit 9263eba

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

apps/cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## NEXT
22

33
- fix: Pass `flutter_assets` to backend when using `celest deploy`
4+
- fix: Sentry integration
45

56
## 1.0.10+2
67

apps/cli/lib/src/cli/cli.dart

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ final class Cli {
8484
(sentryDsn == null
8585
? ctx.httpClient
8686
: SentryHttpClient(client: ctx.httpClient));
87-
ctx.performance = (sentryDsn == null ||
88-
io.Platform.environment.containsKey('CELEST_NO_ANALYTICS'))
89-
? const CelestPerformance()
90-
: const SentryPerformance();
87+
ctx.performance = sentryDsn != null &&
88+
!io.Platform.environment.containsKey('CELEST_NO_ANALYTICS')
89+
? const SentryPerformance()
90+
: const CelestPerformance();
9191
ctx.storage = storage ?? Storage();
9292

9393
try {
@@ -114,18 +114,14 @@ final class Cli {
114114
ctx.secureStorage = NativeMemoryStorage(namespace: Storage.cliNamespace);
115115
}
116116

117-
ctx.analytics = postHogConfig == null ||
118-
io.Platform.environment.containsKey('CELEST_NO_ANALYTICS')
119-
? const NoopAnalytics()
120-
: PostHog(
117+
ctx.analytics = postHogConfig != null &&
118+
!io.Platform.environment.containsKey('CELEST_NO_ANALYTICS')
119+
? PostHog(
121120
config: postHogConfig,
122121
client: ctx.httpClient,
123122
storage: ctx.secureStorage,
124-
);
125-
126-
if (kReleaseMode) {
127-
ctx.analytics.identifyUser(setOnce: {'local_iterations_mvp': true});
128-
}
123+
)
124+
: const NoopAnalytics();
129125

130126
final sdkFinder = DartSdkFinder(
131127
platform: ctx.platform,
@@ -225,6 +221,8 @@ final class Cli {
225221
);
226222
},
227223
appRunner: () => _run(argResults),
224+
// We use our own error handling, so we don't want to use the default
225+
// `runZonedGuarded` behavior.
228226
// ignore: invalid_use_of_internal_member
229227
callAppRunnerInRunZonedGuarded: false,
230228
);
@@ -359,6 +357,13 @@ final class Cli {
359357
}
360358

361359
Future<void> _handleError(Object error, StackTrace stackTrace) async {
360+
Future<void> recordError() async {
361+
await ctx.performance
362+
.innerCaptureError(error, stackTrace: stackTrace)
363+
.timeout(const Duration(seconds: 3))
364+
.catchError((_) => '');
365+
}
366+
362367
switch (error) {
363368
case UsageException():
364369
stderr.writeln(error);
@@ -374,13 +379,15 @@ final class Cli {
374379
..writeln(stackTrace);
375380
}
376381
exitCode = 1;
382+
await recordError();
377383
default:
378384
_logFile
379385
?..writeln(error)
380386
..writeln(stackTrace);
381387
ctx.cliLogger
382388
..err(error.toString())
383389
..detail(stackTrace.toString());
390+
await recordError();
384391
}
385392
}
386393

@@ -393,6 +400,7 @@ final class Cli {
393400
await _loggerSub?.cancel();
394401
await _logFile?.flush();
395402
await _logFile?.close();
403+
await ctx.performance.close();
396404
ctx.httpClient.close();
397405
exit(exitCode);
398406
}

apps/cli/lib/src/context.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
44
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
55
import 'package:celest_cli/src/analytics/interface.dart';
66
import 'package:celest_cli/src/analytics/noop.dart';
7+
import 'package:celest_cli/src/cli/cli_runtime.dart';
78
import 'package:celest_cli/src/commands/auth/cli_auth.dart';
89
import 'package:celest_cli/src/database/cache/cache_database.dart';
910
import 'package:celest_cli/src/database/project/project_database.dart';
@@ -17,7 +18,6 @@ import 'package:celest_cli/src/storage/storage.dart';
1718
import 'package:celest_cli/src/types/type_helper.dart';
1819
import 'package:celest_cli/src/version.dart';
1920
import 'package:celest_cloud/celest_cloud.dart';
20-
import 'package:celest_core/_internal.dart';
2121
import 'package:file/file.dart';
2222
import 'package:file/local.dart';
2323
import 'package:http/http.dart' as http;
@@ -124,7 +124,10 @@ String? get celestLocalPath {
124124
/// The identifier for the current CLI environment.
125125
///
126126
/// This should be used in Sentry/PostHog to identify events.
127-
const String kCliEnvironment = kReleaseMode ? 'release' : 'debug';
127+
final String kCliEnvironment = switch (CliRuntime.current) {
128+
CliRuntime.aot || CliRuntime.pubGlobal => 'release',
129+
CliRuntime.local => 'debug',
130+
};
128131

129132
/// Whether the current terminal supports ANSI output.
130133
///

apps/cli/lib/src/performance/local_perf.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,8 @@ base class CelestPerformance {
3939
);
4040
return result;
4141
}
42+
43+
Future<void> close() async {
44+
// No-op
45+
}
4246
}

apps/cli/lib/src/performance/sentry_perf.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,11 @@ base class SentryPerformance extends CelestPerformance {
5454
await transaction.finish();
5555
}
5656
}
57+
58+
@override
59+
Future<void> close() async {
60+
if (Sentry.isEnabled) {
61+
await Sentry.close();
62+
}
63+
}
5764
}

0 commit comments

Comments
 (0)