From d0af01067bd3301bda65234ea16a5925dfe8b62a Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sun, 24 Sep 2023 23:16:23 +0200 Subject: [PATCH] feat(dynamite): gracefully handle multiple mimeTypes and default responses Signed-off-by: Nikolas Rimikis --- .../dynamite/dynamite/lib/src/builder/client.dart | 6 +++++- .../dynamite/lib/src/builder/resolve_mime_type.dart | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/builder/client.dart b/packages/dynamite/dynamite/lib/src/builder/client.dart index a5d6b3db..2eb555b5 100644 --- a/packages/dynamite/dynamite/lib/src/builder/client.dart +++ b/packages/dynamite/dynamite/lib/src/builder/client.dart @@ -193,7 +193,11 @@ Iterable buildTags( var responses = >{}; if (operation.responses != null) { for (final responseEntry in operation.responses!.entries) { - final statusCode = int.parse(responseEntry.key); + final statusCode = int.tryParse(responseEntry.key); + if (statusCode == null) { + print('Default responses are not supported right now. Skipping it for $operationId'); + continue; + } final response = responseEntry.value; responses[response] ??= []; diff --git a/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart b/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart index 36d38222..5da0f29c 100644 --- a/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart +++ b/packages/dynamite/dynamite/lib/src/builder/resolve_mime_type.dart @@ -15,7 +15,7 @@ TypeResult? resolveMimeTypeDecode( ) { if (response.content != null) { if (response.content!.length > 1) { - throw Exception('Can not work with multiple mime types right now'); + print('Can not work with multiple mime types right now. Using the first supported.'); } for (final content in response.content!.entries) { @@ -35,10 +35,9 @@ TypeResult? resolveMimeTypeDecode( return TypeResultBase('String'); } else if (mimeType == 'application/json') { return result; - } else { - throw Exception('Can not parse mime type "$mimeType"'); } } + throw Exception('Can not parse any mime type of Operation: "$identifier"'); } return null; } @@ -52,7 +51,7 @@ Iterable resolveMimeTypeEncode( ) sync* { if (operation.requestBody != null) { if (operation.requestBody!.content!.length > 1) { - throw Exception('Can not work with multiple mime types right now'); + print('Can not work with multiple mime types right now. Using the first supported.'); } for (final content in operation.requestBody!.content!.entries) { final mimeType = content.key; @@ -121,9 +120,9 @@ Iterable resolveMimeTypeEncode( yield '}'; } return; - default: - throw Exception('Can not parse mime type "$mimeType"'); } } + + throw Exception('Can not parse any mime type of Operation: "$identifier"'); } }