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(api): web socket error handling #5359

Merged
merged 2 commits into from
Aug 23, 2024
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
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
Loading