Skip to content

Commit

Permalink
Early error checking and file name generation
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jun 22, 2023
1 parent b3797c1 commit bfd334d
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions Bonsai.System/IO/FileSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ protected IObservable<TElement> Process<TElement>(
Func<TElement, TSource> selector,
string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
throw new InvalidOperationException("A valid file path must be specified.");
}

if (source == null)
{
throw new ArgumentNullException(nameof(source));
Expand All @@ -129,6 +134,13 @@ protected IObservable<TElement> Process<TElement>(

return Observable.Create<TElement>(observer =>
{
PathHelper.EnsureDirectory(fileName);
fileName = PathHelper.AppendSuffix(fileName, Suffix);
if (File.Exists(fileName) && !Overwrite)
{
throw new IOException(string.Format("The file '{0}' already exists.", fileName));
}
var disposable = new WriterDisposable<TWriter>(Buffered);
var process = source.Do(element =>
{
Expand All @@ -140,18 +152,6 @@ protected IObservable<TElement> Process<TElement>(
var runningWriter = disposable.Writer;
if (runningWriter == null)
{
if (string.IsNullOrEmpty(fileName))
{
throw new InvalidOperationException("A valid file path must be specified.");
}
PathHelper.EnsureDirectory(fileName);
fileName = PathHelper.AppendSuffix(fileName, Suffix);
if (File.Exists(fileName) && !Overwrite)
{
throw new IOException(string.Format("The file '{0}' already exists.", fileName));
}
runningWriter = disposable.Writer = CreateWriter(fileName, input);
}
Expand Down

0 comments on commit bfd334d

Please sign in to comment.