From c14aca4b4bc726181d6ed06dc52049e34f36e5ce Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 8 Mar 2024 16:27:55 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20Throw=20argument=20error=20with?= =?UTF-8?q?=20invalid=20`baseUrl`=20(#2127)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide a readable error when the `baseUrl` is not matching the condition. ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [ ] I have added the required tests to prove the fix/feature I'm adding - [ ] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [ ] I have updated the `CHANGELOG.md` in the corresponding package --- dio/lib/src/options.dart | 8 +++++++- dio/test/options_test.dart | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/dio/lib/src/options.dart b/dio/lib/src/options.dart index aa8d2f96d..8631b7135 100644 --- a/dio/lib/src/options.dart +++ b/dio/lib/src/options.dart @@ -99,7 +99,13 @@ mixin OptionsMixin { late String _baseUrl; set baseUrl(String value) { - assert(value.isEmpty || kIsWeb || Uri.parse(value).host.isNotEmpty); + if (value.isNotEmpty && !kIsWeb && Uri.parse(value).host.isEmpty) { + throw ArgumentError.value( + value, + 'baseUrl', + 'Must be a valid URL on platforms other than Web.', + ); + } _baseUrl = value; } diff --git a/dio/test/options_test.dart b/dio/test/options_test.dart index 159e597ed..960029430 100644 --- a/dio/test/options_test.dart +++ b/dio/test/options_test.dart @@ -482,7 +482,7 @@ void main() { if (kIsWeb) 'api/', ]; for (final url in invalidUrls) { - expect(() => BaseOptions(baseUrl: url), throwsA(isA())); + expect(() => BaseOptions(baseUrl: url), throwsA(isA())); } for (final url in validUrls) { expect(BaseOptions(baseUrl: url), isA());