Skip to content

Commit 36b05ac

Browse files
feat: Enable adding custom data to diagnostic events (serverpod#3363)
This adds the ability to include custom data when submitting diagnostic events. It is intended to be used by application (user) code, not framework code. The content to include and how to handle it in the event handlers is up to the user. Closes serverpod#3332 The solution differs slightly from the suggestion in the issue: Rather than extending the context, this is included as a key/value map in the event itself. This makes sense since the context types are framework-managed and this is an entirely user-owned data payload for individual events. ## Pre-launch Checklist - [x] I read the [Contribute](https://docs.serverpod.dev/contribute) page and followed the process outlined there for submitting PRs. - [x] This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.) - [x] I read and followed the [Dart Style Guide](https://dart.dev/guides/language/effective-dart/style) and formatted the code with [dart format](https://dart.dev/tools/dart-format). - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar. - [x] I added new tests to check the change I am making. - [x] All existing and new tests are passing. - [x] Any breaking changes are documented below. If you need help, consider asking for advice on the [discussion board](https://github.com/serverpod/serverpod/discussions). ## Breaking changes No, does not change any existing API. Adds an optional initialization field to the diagnostic event constructor.
1 parent 4e30de5 commit 36b05ac

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

packages/serverpod/lib/src/server/diagnostic_events/event_handler.dart

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ enum OriginSpace {
1010
}
1111

1212
/// Base interface for all diagnostic events.
13-
abstract interface class DiagnosticEvent {}
13+
abstract interface class DiagnosticEvent {
14+
/// Custom key/value data added to the event.
15+
///
16+
/// It is up to application code submitting events
17+
/// and event handler implementations how to use this data.
18+
Map<String, Object> get custom;
19+
}
1420

1521
/// An event that represents an exception that occurred in the server.
1622
class ExceptionEvent implements DiagnosticEvent {
@@ -23,11 +29,15 @@ class ExceptionEvent implements DiagnosticEvent {
2329
/// An optional message associated with the exception.
2430
final String? message;
2531

32+
@override
33+
final Map<String, Object> custom;
34+
2635
/// Creates a new [ExceptionEvent].
2736
const ExceptionEvent(
2837
this.exception,
2938
this.stackTrace, {
3039
this.message,
40+
this.custom = const {},
3141
});
3242

3343
/// Returns a string representation of this context.

tests/serverpod_test_server/lib/src/endpoints/diagnostic_event_test_endpoint.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ class DiagnosticEventTestEndpoint extends Endpoint {
66
throw Exception('An exception is thrown');
77
} catch (e, stackTrace) {
88
session.serverpod.experimental.submitDiagnosticEvent(
9-
ExceptionEvent(e, stackTrace),
9+
ExceptionEvent(
10+
e,
11+
stackTrace,
12+
custom: {'customKey': 'customValue'},
13+
),
1014
session: session,
1115
);
1216
}

tests/serverpod_test_server/test_integration/diagnostics/diagnostic_events_api_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void main() {
2222

2323
final record = await exceptionHandler.events.first.timeout(timeout);
2424
expect(record.event.exception, isA<Exception>());
25+
expect(record.event.custom, equals({'customKey': 'customValue'}));
2526
expect(record.space, equals(OriginSpace.application));
2627
expect(record.context, isA<DiagnosticEventContext>());
2728
expect(

0 commit comments

Comments
 (0)