Improve invalid_mime_type error to report the attempted mime type #403
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Improve error message clarity
What is the current behavior?
When uploading an object to supabase storage, the mime type is checked against the allowed mime types on the bucket. If the mime type is not allowed, an error message is reported, but the message does not say what the mime type was.
Before:
What is the new behavior?
If the mime type is not allowed, the message informs the user what mime type was attempted:
Additional context
I got stuck on a really hairy problem today that took quite a bit of digging to figure out. Here's what happened:
fetch()
to retrieve a photo from a URL. This resulted in aBlob
type.upload()
to put that image into object storage. (Note: I already had thecontentType
variable from another source, so I expected that this file would be uploaded asimage/jpeg
)However, for some reason, the server from which I was retrieving the photo was returning
Content-type: application/octet-stream
. See this community post on AirtableThe
Blob
data type already includes a mime type, and the Supabase storage API uses this instead of the explicitly declaredcontentType
parameter! This means that, even though I was explicitly specifying the content type, I was inadvertently uploading my file with theapplication/octet-stream
mime-type, which my bucket did not allow!There were no logs or error messages to indicate that I was attempting to upload an
application/octet-stream
content type. I checked the Supabase storage logs, and there was no error message indicating a failed upload, and the context was not in the error message. With the content type explicitly specified asimage/jpeg
, and with no other clues that the upload was actually going out asapplication/octet-stream
, I was completely stumped.I was finally able to get to the bottom of this based on this clue from the supabase docs. This line made me try leaving out the
contentType
parameter, and then that finally got me to log theblob.type
field. Once I converted the blob to a stream and specified the correct content type, the upload worked.It took me many hours to get to the bottom of why this was happening. For this pull request, I opted not to try to make the
contentType
parameter take precedence over theBlob
's content type. I think it would make more sense to use the explicitly declaredcontentType
field, but I didn't want to change behavior in case there was a good reason why it works this way. However, I did try to make the resulting error message a little more explicit. If the error had told me what the attempted mime type was, I think I would have realized what was happening a lot sooner.I also updated the
mime type is not formatted properly
message, and added a test for it.