Skip to content

Commit

Permalink
fix(api): web socket error handling (#5359)
Browse files Browse the repository at this point in the history
  • Loading branch information
Equartey authored Aug 23, 2024
1 parent db95ad0 commit 995e6bd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,15 @@ class WebSocketError extends WebSocketMessagePayload implements Exception {
final List<Map<String, dynamic>> errors;

static WebSocketError fromJson(Map<String, dynamic> json) {
final errors = json['errors'] as List?;
return WebSocketError(errors?.cast() ?? []);
final errors = json['errors'];
List<Map<String, dynamic>>? errorsList = [];
if (errors is List?) {
errorsList = errors?.cast();
} else if (errors is Map<String, dynamic>) {
errorsList = [errors];
}

return WebSocketError(errorsList ?? []);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ void main() {
errors,
);

/// GraphQLResponseDecoder should handle a payload with errors.
final response = GraphQLResponseDecoder.instance.decode<String>(
request: GraphQLRequest<String>(
document: '',
),
response: message.payload!.toJson(),
);
expect(
response.errors.first.message,
errorMessage,
);
});
test('WebsocketMessage should decode errors as a Map', () {
const errorMessage = 'Max number of 100 subscriptions reached';
const errorType = 'MaxSubscriptionsReachedError';
const errorMap = {'errorType': errorType, 'message': errorMessage};
final entry = {
'id': 'xyz-456',
'type': 'error',
'payload': {'data': null, 'errors': errorMap},
};
final message = WebSocketMessage.fromJson(entry);
expect(message.messageType, MessageType.error);
expect(
message.payload!.toJson()['errors'],
[errorMap],
);

/// GraphQLResponseDecoder should handle a payload with errors.
final response = GraphQLResponseDecoder.instance.decode<String>(
request: GraphQLRequest<String>(
Expand Down

0 comments on commit 995e6bd

Please sign in to comment.