Dynamic custom error pages for mimetypes do not work if multiple mimetypes were given in the Accept header #10974
Labels
kind/feature
Categorizes issue or PR as related to a new feature.
lifecycle/frozen
Indicates that an issue or PR should not be auto-closed due to staleness.
needs-priority
needs-triage
Indicates an issue or PR lacks a `triage/foo` label and requires one.
We implemented custom error pages for requests that expect a JSON response.
This however only works if the
Accept:
header consists of only one mimetype, in this caseapplication/json
.If you add additional mimetypes in your request what seems to be quite common the
errorHandler
can not resolve the mimetype to an extension anymore and just returns the default custom error pages which are usually a HTML representation.The following examples show what the current state is:
Works as expected:
Works as expected:
Does not work as expected: (should also return a custom JSON error page
403.json
)The reason for that is simple. The Go function
mime.ExtensionsByType
andmime.ParseMediaType
do not like multiple mimetypes and just return an empty list. Here's a small example and its output:Output:
Possible solution
Preparse the
Accept
header and only take the first mimetype that's mentioned there. This should be quite easy to do. Just split the string by comma and take the first element before handing it over to theerrorHandler
. Or just do that in theerrorHandler
itself.I by myself am not a Go programmer unfortunately so it would be nice if someone else could implement that.
The text was updated successfully, but these errors were encountered: