Skip to content

Commit

Permalink
relaxed file/folder by name browser acces to throw less errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickodei committed Jan 22, 2025
1 parent 3f449a4 commit b117388
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Browser/Avalonia.Browser/Storage/BrowserStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ internal class BrowserStorageProvider : IStorageProvider
internal static ReadOnlySpan<byte> BrowserBookmarkKey => "browser"u8;
internal const string PickerCancelMessage = "The user aborted a request";
internal const string NoPermissionsMessage = "Permissions denied";
internal const string FileFolderNotFoundMessage = "A requested file or directory could not be found";
internal const string TypeMissmatchMessage = "The path supplied exists, but was not an entry of requested type";

public bool CanOpen => true;
public bool CanSave => true;
Expand Down Expand Up @@ -398,9 +400,9 @@ public async IAsyncEnumerable<IStorageItem> GetItemsAsync()

return new JSStorageFolder(storageFile);
}
catch (JSException ex) when (ex.Message == BrowserStorageProvider.NoPermissionsMessage)
catch (JSException ex) when (ShouldSupressErrorOnFileAccess(ex))
{
throw new UnauthorizedAccessException("User denied permissions to open the folder", ex);
return null;
}
}

Expand All @@ -416,9 +418,14 @@ public async IAsyncEnumerable<IStorageItem> GetItemsAsync()

return new JSStorageFile(storageFile);
}
catch (JSException ex) when (ex.Message == BrowserStorageProvider.NoPermissionsMessage)
catch (JSException ex) when (ShouldSupressErrorOnFileAccess(ex))
{
throw new UnauthorizedAccessException("User denied permissions to open the file", ex);
return null;
}
}

private static bool ShouldSupressErrorOnFileAccess(JSException ex) =>
ex.Message == BrowserStorageProvider.NoPermissionsMessage ||
ex.Message.Contains(BrowserStorageProvider.TypeMissmatchMessage, StringComparison.Ordinal) ||
ex.Message.Contains(BrowserStorageProvider.FileFolderNotFoundMessage, StringComparison.Ordinal);
}

0 comments on commit b117388

Please sign in to comment.