-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): library-level exceptions
- Loading branch information
Showing
12 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
abstract class ClientException implements Exception {} | ||
|
||
abstract class ClientCacheException implements ClientException {} | ||
|
||
/// A failure during the cache's entity normalization processes | ||
class NormalizationException implements ClientCacheException { | ||
NormalizationException(this.cause, this.overflowError, this.value); | ||
|
||
StackOverflowError overflowError; | ||
String cause; | ||
Object value; | ||
|
||
String get message => cause; | ||
} | ||
|
||
/// A failure to find a key in the cache when cacheOnly=true | ||
class CacheMissException implements ClientCacheException { | ||
CacheMissException(this.cause, this.missingKey); | ||
|
||
String cause; | ||
String missingKey; | ||
|
||
String get message => cause; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export './graphql_error.dart'; | ||
export './operation_exception.dart'; | ||
export './network_exception_stub.dart' | ||
if (dart.library.io) './io_network_exceptions.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/graphql/lib/src/exceptions/io_network_exception.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'dart:io' show SocketException; | ||
import './network_exception_stub.dart' as stub; | ||
|
||
class NetworkException extends stub.NetworkException { | ||
SocketException wrappedException; | ||
|
||
NetworkException.from(this.wrappedException); | ||
|
||
String get message => wrappedException.message; | ||
String get targetAddress => wrappedException.address.address; | ||
int get targetPort => wrappedException.port; | ||
} | ||
|
||
void translateExceptions(stub.VoidCallback block) { | ||
try { | ||
block(); | ||
} on SocketException catch (e) { | ||
throw NetworkException.from(e); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/graphql/lib/src/exceptions/network_exception_stub.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import './base_exceptions.dart' show ClientException; | ||
|
||
typedef VoidCallback = void Function(); | ||
|
||
class NetworkException implements ClientException { | ||
covariant Exception wrappedException; | ||
|
||
final String message; | ||
|
||
final String targetAddress; | ||
final int targetPort; | ||
|
||
NetworkException({ | ||
this.wrappedException, | ||
this.message, | ||
this.targetAddress, | ||
this.targetPort, | ||
}); | ||
|
||
String toString() => | ||
'Failed to connect to $targetAddress:$targetPort: $message'; | ||
} | ||
|
||
void translateExceptions(VoidCallback block) => block(); |
44 changes: 44 additions & 0 deletions
44
packages/graphql/lib/src/exceptions/operation_exception.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:graphql/src/exceptions/base_exceptions.dart'; | ||
|
||
import './graphql_error.dart'; | ||
import './network_exception_stub.dart' | ||
if (dart.library.io) './io_network_exceptions.dart'; | ||
|
||
class OperationException implements Exception { | ||
List<GraphQLError> graphqlErrors = []; | ||
|
||
// generalize to include cache error, etc | ||
ClientException clientException; | ||
|
||
OperationException({ | ||
this.clientException, | ||
Iterable<GraphQLError> graphqlErrors = const [], | ||
}) : this.graphqlErrors = graphqlErrors.toList(); | ||
|
||
void addError(GraphQLError error) => graphqlErrors.add(error); | ||
} | ||
|
||
/// `(graphqlErrors?, exception?) => exception?` | ||
/// | ||
/// merges both optional graphqlErrors and an optional container | ||
/// into a single optional container | ||
/// NOTE: NULL returns expected | ||
OperationException coalesceErrors({ | ||
List<GraphQLError> graphqlErrors, | ||
ClientException clientException, | ||
OperationException exception, | ||
}) { | ||
if (exception != null || | ||
clientException != null || | ||
graphqlErrors == null || | ||
graphqlErrors.isEmpty) { | ||
return OperationException( | ||
clientException: clientException ?? exception.clientException, | ||
graphqlErrors: [ | ||
if (graphqlErrors != null) ...graphqlErrors, | ||
if (exception?.graphqlErrors != null) ...exception.graphqlErrors | ||
], | ||
); | ||
} | ||
return null; | ||
} |