Skip to content
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

fix: compile with webdev #653

Merged
merged 3 commits into from
Oct 5, 2023
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
6 changes: 3 additions & 3 deletions packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart';
Expand All @@ -13,7 +14,6 @@ import 'package:http/http.dart';
import 'package:jwt_decode/jwt_decode.dart';
import 'package:meta/meta.dart';
import 'package:rxdart/subjects.dart';
import 'package:universal_io/io.dart';

part 'gotrue_mfa_api.dart';

Expand All @@ -29,7 +29,7 @@ part 'gotrue_mfa_api.dart';
/// [asyncStorage] local storage to store pkce code verifiers. Required when using the pkce flow.
///
/// Set [flowType] to `AuthFlowType.pkce` to perform pkce auth flow.
/// /// {@endtemplate}
/// {@endtemplate}
class GoTrueClient {
/// Namespace for the GoTrue API methods.
/// These can be used for example to get a user from a JWT in a server environment or reset a user's password.
Expand Down Expand Up @@ -966,7 +966,7 @@ class GoTrueClient {
_notifyAllSubscribers(AuthChangeEvent.tokenRefreshed);
_refreshTokenCompleter!.complete(authResponse);
return authResponse;
} on SocketException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about what is going on here. How can the thrown error type change without changing anything within the try block? Wouldn't .request() call on line 942 still throw SocketException when there is an issue with the network?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the doc

If there is a socket-level failure when communicating with the server (for example, if the server could not be reached), IOClient will emit a ClientException that also implements SocketException. This allows callers to get more detailed exception information for socket-level failures, if desired.

So any SocketException from the HttpClient gets caught and put into a ClientException that also implements SocketException, so you can use both to catch the exception. See their send implementation.

} on ClientException {
_setTokenRefreshTimer(
Constants.retryInterval * pow(2, _refreshTokenRetryCount),
refreshToken: token,
Expand Down
1 change: 0 additions & 1 deletion packages/gotrue/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:
crypto: ^3.0.2
http: '>=0.13.0 <2.0.0'
jwt_decode: ^0.3.1
universal_io: ^2.0.4
rxdart: ^0.27.7
meta: ^1.7.0

Expand Down
3 changes: 1 addition & 2 deletions packages/gotrue/test/custom_http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:http/http.dart';
import 'package:universal_io/io.dart';

import 'utils.dart';

Expand Down Expand Up @@ -74,7 +73,7 @@ class RetryTestHttpClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) async {
retryCount++;
if (retryCount < 4) {
throw SocketException('Retry #$retryCount');
throw ClientException('Retry #$retryCount');
}
final jwt = JWT(
{'exp': (DateTime.now().millisecondsSinceEpoch / 1000).round() + 60},
Expand Down
7 changes: 4 additions & 3 deletions packages/storage_client/lib/src/fetch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import 'package:http_parser/http_parser.dart' show MediaType;
import 'package:mime/mime.dart';
import 'package:retry/retry.dart';
import 'package:storage_client/src/types.dart';
import 'package:universal_io/io.dart';

import 'file_io.dart' if (dart.library.js) './file_stub.dart';

Fetch storageFetch = Fetch();

Expand Down Expand Up @@ -108,7 +109,7 @@ class Fetch {
},
retryIf: (error) =>
retryController?.cancelled != true &&
(error is SocketException || error is TimeoutException),
(error is ClientException || error is TimeoutException),
);

return _handleResponse(streamedResponse, options);
Expand Down Expand Up @@ -152,7 +153,7 @@ class Fetch {
},
retryIf: (error) =>
retryController?.cancelled != true &&
(error is SocketException || error is TimeoutException),
(error is ClientException || error is TimeoutException),
);

return _handleResponse(streamedResponse, options);
Expand Down
1 change: 1 addition & 0 deletions packages/storage_client/lib/src/file_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'dart:io' show File;
8 changes: 8 additions & 0 deletions packages/storage_client/lib/src/file_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'dart:typed_data';

/// A stub for the `dart:io` [File] class. Only the methods used by the storage client are stubbed.
class File {
String get path => throw UnimplementedError();

Uint8List readAsBytesSync() => throw UnimplementedError();
}
3 changes: 2 additions & 1 deletion packages/storage_client/lib/src/storage_file_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'dart:typed_data';

import 'package:storage_client/src/fetch.dart';
import 'package:storage_client/src/types.dart';
import 'package:universal_io/io.dart';

import 'file_io.dart' if (dart.library.js) './file_stub.dart';

class StorageFileApi {
final String url;
Expand Down
1 change: 0 additions & 1 deletion packages/storage_client/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies:
http: '>=0.13.4 <2.0.0'
http_parser: ^4.0.1
mime: ^1.0.2
universal_io: ^2.0.4
retry: ^3.1.0

dev_dependencies:
Expand Down
3 changes: 1 addition & 2 deletions packages/storage_client/test/custom_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:universal_io/io.dart';

class CustomHttpClient extends BaseClient {
@override
Expand All @@ -22,7 +21,7 @@ class RetryHttpClient extends BaseClient {
Future<StreamedResponse> send(BaseRequest request) async {
if (failureCount < 3) {
failureCount++;
throw SocketException('Offline');
throw ClientException('Offline');
}
//Return custom status code to check for usage of this client.
return StreamedResponse(
Expand Down
Loading