diff --git a/pkgs/web_socket/CHANGELOG.md b/pkgs/web_socket/CHANGELOG.md index 0da1b0f6d6..df813feba0 100644 --- a/pkgs/web_socket/CHANGELOG.md +++ b/pkgs/web_socket/CHANGELOG.md @@ -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`. diff --git a/pkgs/web_socket/lib/src/web_socket.dart b/pkgs/web_socket/lib/src/web_socket.dart index f3f86feb32..76cbb888a4 100644 --- a/pkgs/web_socket/lib/src/web_socket.dart +++ b/pkgs/web_socket/lib/src/web_socket.dart @@ -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. diff --git a/pkgs/web_socket/pubspec.yaml b/pkgs/web_socket/pubspec.yaml index a5d56acee5..9bca7f5c03 100644 --- a/pkgs/web_socket/pubspec.yaml +++ b/pkgs/web_socket/pubspec.yaml @@ -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 diff --git a/pkgs/web_socket/test/exceptions_test.dart b/pkgs/web_socket/test/exceptions_test.dart new file mode 100644 index 0000000000..5ed2711a76 --- /dev/null +++ b/pkgs/web_socket/test/exceptions_test.dart @@ -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'); + }); + }); +}