Skip to content

Commit 7a2e7d5

Browse files
authored
Add a useful stringificiation to WebSocketConnectionClosed (#1764)
1 parent ccb6533 commit 7a2e7d5

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

pkgs/web_socket/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.1-wip
2+
3+
- Fix a bug where `WebSocketException`/`WebSocketConnectionClosed` did not
4+
have a useful string representation.
5+
16
## 1.0.0
27

38
- First non-experimental release; no semantic changes from version `0.1.6`.

pkgs/web_socket/lib/src/web_socket.dart

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ class WebSocketException implements Exception {
9898
/// [WebSocket.close] is called when the [WebSocket] is closed.
9999
class WebSocketConnectionClosed extends WebSocketException {
100100
WebSocketConnectionClosed([super.message = 'Connection Closed']);
101+
102+
@override
103+
String toString() {
104+
if (message.isEmpty) {
105+
return 'WebSocketConnectionClosed';
106+
} else {
107+
return 'WebSocketConnectionClosed: $message';
108+
}
109+
}
101110
}
102111

103112
/// The interface for WebSocket connections.

pkgs/web_socket/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
Any easy-to-use library for communicating with WebSockets
44
that has multiple implementations.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/web_socket
6-
version: 1.0.0
6+
version: 1.0.1-wip
77

88
environment:
99
sdk: ^3.4.0
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:test/test.dart';
6+
import 'package:web_socket/web_socket.dart';
7+
8+
void main() {
9+
group('WebSocketException', () {
10+
test('no message', () async {
11+
final exception = WebSocketException();
12+
expect(exception.message, isEmpty);
13+
expect(exception.toString(), 'WebSocketException');
14+
});
15+
16+
test('with message', () async {
17+
final exception = WebSocketException('bad connection');
18+
expect(exception.message, 'bad connection');
19+
expect(exception.toString(), 'WebSocketException: bad connection');
20+
});
21+
});
22+
23+
group('WebSocketConnectionClosed', () {
24+
test('no message', () async {
25+
final exception = WebSocketConnectionClosed();
26+
expect(exception.message, 'Connection Closed');
27+
expect(
28+
exception.toString(), 'WebSocketConnectionClosed: Connection Closed');
29+
});
30+
31+
test('empty message', () async {
32+
final exception = WebSocketConnectionClosed('');
33+
expect(exception.message, isEmpty);
34+
expect(exception.toString(), 'WebSocketConnectionClosed');
35+
});
36+
37+
test('with message', () async {
38+
final exception = WebSocketConnectionClosed('bad connection');
39+
expect(exception.message, 'bad connection');
40+
expect(exception.toString(), 'WebSocketConnectionClosed: bad connection');
41+
});
42+
});
43+
}

0 commit comments

Comments
 (0)