Skip to content

Commit

Permalink
🥅 Catch MediaType parse exception in Transformer.isJsonMimeType (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Jun 25, 2024
1 parent 0d06244 commit 224db94
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 221 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Improves `InterceptorState.toString()`.
- If the `CancelToken` got canceled before making requests,
throws the exception directly rather than cut actual HTTP requests afterward.
- Catch `MediaType` parse exception in `Transformer.isJsonMimeType`.

## 5.4.3+1

Expand Down
17 changes: 13 additions & 4 deletions dio/lib/src/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,19 @@ abstract class Transformer {
if (contentType == null) {
return false;
}
final mediaType = MediaType.parse(contentType);
return mediaType.mimeType == 'application/json' ||
mediaType.mimeType == 'text/json' ||
mediaType.subtype.endsWith('+json');
try {
final mediaType = MediaType.parse(contentType);
return mediaType.mimeType == 'application/json' ||
mediaType.mimeType == 'text/json' ||
mediaType.subtype.endsWith('+json');
} catch (e, s) {
debugLog(
'Failed to parse the media type: $contentType, '
'thus it is not a JSON MIME type.',
s,
);
return false;
}
}

static FutureOr<String> defaultTransformRequest(
Expand Down
Loading

0 comments on commit 224db94

Please sign in to comment.