Catch exceptions while loading remote files

This commit is contained in:
ds5678 2025-03-30 23:07:33 -07:00
parent 07563989e1
commit 48bd179426

View File

@ -52,6 +52,8 @@ internal static class RoutingExtensions
internal static RouteHandlerBuilder MapRemoteFile(this IEndpointRouteBuilder endpoints, [StringSyntax("Route")] string path, string contentType, string source, string? integrity = null)
{
return endpoints.MapGet(path, async (context) =>
{
try
{
string fileName = Path.GetFileName(path);
byte[] data = await StaticContentLoader.LoadRemote(path, source, integrity);
@ -63,6 +65,11 @@ internal static class RoutingExtensions
{
await Results.Bytes(data, contentType, fileName).ExecuteAsync(context);
}
}
catch (Exception ex)
{
await Results.InternalServerError(ex.ToString()).ExecuteAsync(context);
}
}).Produces<byte[]>(StatusCodes.Status200OK, contentType);
}
}