KEMBAR78
InvalidArgumentException from HttpResponse if the content-type is invalid · Issue #905 · googleapis/google-http-java-client · GitHub
Skip to content

InvalidArgumentException from HttpResponse if the content-type is invalid #905

@jmorrise

Description

@jmorrise

I'm hitting an edge case at HttpResponse.java#153: if the contentType of the response is invalid, an InvalidArgumentException is thrown while constructing the media type. My code makes an http request to read the contents of a file from Google Cloud Storage, so if the owner of the file has set an invalid content-type (which is easy to do in GCS, unfortunately), I can't read the file at all.

I understand that this might just be WAI, and I could just throw the error back to the user, however it would be nice if I could handle this gracefully and read the file contents as if the mediaType were unknown. It's difficult for me to catch and handle the error myself since I'm not calling this library directly (another library that I don't own is making the http request).

Since mediaType is nullable anyway, would it be reasonable behavior to set it to null if the contentType were invalid? Something like

mediaType = contentType == null || !HttpMediaType.isValid(contentType) ? null : new HttpMediaType(contentType);

where isValid would return true iff the contentType can be parsed to a valid mediaType.

Would this be an okay change? I wanted to check before actually sending a pull request. Thanks.

Metadata

Metadata

Assignees

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions