Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,22 @@ public async Task List_ShouldSurfaceStorageException_GivenNonJsonError()
}
}

[TestMethod]
public async Task List_ShouldSurfaceStorageException_GivenNonJsonSuccessBody()
{
const string body = "<html><body>502 Bad Gateway</body></html>";
this.server.Given(Request.Create().WithPath($"/storage/v1/object/list/{Bucket}").UsingPost())
.RespondWith(Response.Create().WithStatusCode(200).WithBody(body));
var act = () => this.client.From(Bucket).List();
var exception = (await act.Should().ThrowAsync<SupabaseStorageException>(
"storage-js reports a non-JSON 2xx body as a StorageUnknownError, not a raw parse error")).Which;
using (new AssertionScope())
{
exception.StatusCode.Should().Be(200);
exception.Content.Should().Be(body);
}
}

[TestMethod]
public async Task UploadToSignedUrl_ShouldForwardMetadataAndCustomHeaders_GivenFileOptions()
{
Expand Down
15 changes: 15 additions & 0 deletions packages/Storage/Storage/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ internal static HttpClient ResolveDownloadClient(ClientOptions options) =>
var response = await MakeRequestAsync(httpClient, retry, method, url, data, headers, cancellationToken);
var content = await response.Content.ReadAsStringAsync();

// A proxy can answer 2xx with an HTML page; JSON that doesn't fit T still throws below.
try
{
JsonDocument.Parse(content).Dispose();
}
catch (JsonException e)
{
throw new SupabaseStorageException(e.Message, e)
{
Content = content,
Response = response,
StatusCode = (int) response.StatusCode,
};
}

return JsonSerializer.Deserialize<T>(content, SerializerOptions);
}

Expand Down