Skip to content

Commit

Permalink
🥅 Throw argument error with invalid baseUrl (#2127)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AlexV525 authored Mar 8, 2024
1 parent ccc7666 commit c14aca4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ void main() {
if (kIsWeb) 'api/',
];
for (final url in invalidUrls) {
expect(() => BaseOptions(baseUrl: url), throwsA(isA<AssertionError>()));
expect(() => BaseOptions(baseUrl: url), throwsA(isA<ArgumentError>()));
}
for (final url in validUrls) {
expect(BaseOptions(baseUrl: url), isA<BaseOptions>());
Expand Down

0 comments on commit c14aca4

Please sign in to comment.