Skip to content

Add a useful stringificiation to WebSocketConnectionClosed #1764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkgs/web_socket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.1-wip

- Fix a bug where `WebSocketException`/`WebSocketConnectionClosed` did not
have a useful string representation.

## 1.0.0

- First non-experimental release; no semantic changes from version `0.1.6`.
Expand Down
9 changes: 9 additions & 0 deletions pkgs/web_socket/lib/src/web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ class WebSocketException implements Exception {
/// [WebSocket.close] is called when the [WebSocket] is closed.
class WebSocketConnectionClosed extends WebSocketException {
WebSocketConnectionClosed([super.message = 'Connection Closed']);

@override
String toString() {
if (message.isEmpty) {
return 'WebSocketConnectionClosed';
} else {
return 'WebSocketConnectionClosed: $message';
}
}
}

/// The interface for WebSocket connections.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/web_socket/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >-
Any easy-to-use library for communicating with WebSockets
that has multiple implementations.
repository: https://github.com/dart-lang/http/tree/master/pkgs/web_socket
version: 1.0.0
version: 1.0.1-wip

environment:
sdk: ^3.4.0
Expand Down
43 changes: 43 additions & 0 deletions pkgs/web_socket/test/exceptions_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:test/test.dart';
import 'package:web_socket/web_socket.dart';

void main() {
group('WebSocketException', () {
test('no message', () async {
final exception = WebSocketException();
expect(exception.message, isEmpty);
expect(exception.toString(), 'WebSocketException');
});

test('with message', () async {
final exception = WebSocketException('bad connection');
expect(exception.message, 'bad connection');
expect(exception.toString(), 'WebSocketException: bad connection');
});
});

group('WebSocketConnectionClosed', () {
test('no message', () async {
final exception = WebSocketConnectionClosed();
expect(exception.message, 'Connection Closed');
expect(
exception.toString(), 'WebSocketConnectionClosed: Connection Closed');
});

test('empty message', () async {
final exception = WebSocketConnectionClosed('');
expect(exception.message, isEmpty);
expect(exception.toString(), 'WebSocketConnectionClosed');
});

test('with message', () async {
final exception = WebSocketConnectionClosed('bad connection');
expect(exception.message, 'bad connection');
expect(exception.toString(), 'WebSocketConnectionClosed: bad connection');
});
});
}
Loading