Skip to content

Commit 5dcbd0c

Browse files
raquelsabhsubra
authored andcommitted
Treating exceptions at provider/catalog methods
.
1 parent a339b08 commit 5dcbd0c

File tree

7 files changed

+163
-115
lines changed

7 files changed

+163
-115
lines changed

src/LibraryManager.Vsix/UI/InstallDialog.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ internal InstallDialogViewModel ViewModel
5151

5252
public Task<CompletionSet> PerformSearch(string searchText, int caretPosition)
5353
{
54-
return ViewModel.SelectedProvider.GetCatalog().GetLibraryCompletionSetAsync(searchText, caretPosition);
54+
try
55+
{
56+
return ViewModel.SelectedProvider.GetCatalog().GetLibraryCompletionSetAsync(searchText, caretPosition);
57+
}
58+
catch (InvalidLibraryException ex)
59+
{
60+
// Make the warning visible with ex.Message
61+
return Task.FromResult<CompletionSet>(default(CompletionSet));
62+
}
5563
}
5664

5765
public Task<CompletionSet> TargetLocationSearch(string searchText, int caretPosition)

src/LibraryManager/Providers/Cdnjs/CdnjsCatalog.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,10 @@ public async Task<ILibrary> GetLibraryAsync(string libraryId, CancellationToken
129129
{
130130
try
131131
{
132-
string[] args = libraryId.Split('@');
133-
string name = args[0];
134-
string version = (args.Count() > 1) ? args[1] :null;
132+
string name = _provider.GetLibraryName(libraryId);
133+
string version = _provider.GetLibraryVersion(libraryId);
135134

136135
IEnumerable<Asset> assets = await GetAssetsAsync(name, cancellationToken).ConfigureAwait(false);
137-
138136
Asset asset = assets.FirstOrDefault(a => a.Version == version);
139137

140138
if (asset == null)
@@ -326,4 +324,4 @@ internal class Asset
326324
public string[] Files { get; set; }
327325
public string DefaultFile { get; set; }
328326
}
329-
}
327+
}

src/LibraryManager/Providers/Cdnjs/CdnjsProvider.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ public async Task<ILibraryOperationResult> UpdateStateAsync(ILibraryInstallation
178178

179179
if (library == null)
180180
{
181-
throw new InvalidLibraryException(desiredState.LibraryId, Id);
181+
return new LibraryOperationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId));
182182
}
183183

184184
if (desiredState.Files != null && desiredState.Files.Count > 0)
185185
{
186186
IReadOnlyList<string> invalidFiles = library.GetInvalidFiles(desiredState.Files);
187187
if (invalidFiles.Any())
188188
{
189-
var invalidFilesError = PredefinedErrors.InvalidFilesInLibrary(desiredState.LibraryId, invalidFiles, library.Files.Keys);
189+
IError invalidFilesError = PredefinedErrors.InvalidFilesInLibrary(desiredState.LibraryId, invalidFiles, library.Files.Keys);
190190
return new LibraryOperationResult(desiredState, invalidFilesError);
191191
}
192192
else
@@ -203,7 +203,7 @@ public async Task<ILibraryOperationResult> UpdateStateAsync(ILibraryInstallation
203203
Files = library.Files.Keys.ToList(),
204204
};
205205
}
206-
catch (Exception ex) when (ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException)
206+
catch (InvalidLibraryException)
207207
{
208208
return new LibraryOperationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId));
209209
}
@@ -222,8 +222,8 @@ public async Task<ILibraryOperationResult> UpdateStateAsync(ILibraryInstallation
222222

223223
private async Task<Stream> GetStreamAsync(ILibraryInstallationState state, string sourceFile, CancellationToken cancellationToken)
224224
{
225-
string name = GetLibraryName(state);
226-
string version = GetLibraryVersion(state);
225+
string name = GetLibraryName(state.LibraryId);
226+
string version = GetLibraryVersion(state.LibraryId);
227227

228228
string absolute = Path.Combine(CacheFolder, name, version, sourceFile);
229229

@@ -235,17 +235,17 @@ private async Task<Stream> GetStreamAsync(ILibraryInstallationState state, strin
235235
return null;
236236
}
237237

238-
private string GetLibraryName(ILibraryInstallationState state)
238+
public string GetLibraryName(string libraryId)
239239
{
240-
string[] args = state.LibraryId.Split('@');
240+
string[] args = libraryId.Split('@');
241241
string name = args[0];
242242

243243
return name;
244244
}
245245

246-
private string GetLibraryVersion(ILibraryInstallationState state)
246+
public string GetLibraryVersion(string libraryId)
247247
{
248-
string[] args = state.LibraryId.Split('@');
248+
string[] args = libraryId.Split('@');
249249
string version = args[1];
250250

251251
return version;
@@ -265,9 +265,8 @@ private async Task<ILibraryOperationResult> RefreshCacheAsync(ILibraryInstallati
265265
}
266266

267267
var tasks = new List<Task>();
268-
string[] args = state.LibraryId.Split('@');
269-
string name = args[0];
270-
string version = args[1];
268+
string name = GetLibraryName(state.LibraryId);
269+
string version = GetLibraryVersion(state.LibraryId);
271270

272271
string libraryDir = Path.Combine(CacheFolder, name);
273272

@@ -307,9 +306,8 @@ private async Task<ILibraryOperationResult> RefreshCacheAsync(ILibraryInstallati
307306

308307
private bool IsLibraryUpToDateAsync(ILibraryInstallationState state, CancellationToken cancellationToken)
309308
{
310-
string[] args = state.LibraryId.Split('@');
311-
string name = args[0];
312-
string version = args[1];
309+
string name = GetLibraryName(state.LibraryId);
310+
string version = GetLibraryVersion(state.LibraryId);
313311

314312
string cacheDir = Path.Combine(CacheFolder, name, version);
315313
string destinationDir = Path.Combine(HostInteraction.WorkingDirectory, state.DestinationPath);

src/LibraryManager/Providers/FileSystem/FileSystemCatalog.cs

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,55 +44,62 @@ public Task<CompletionSet> GetLibraryCompletionSetAsync(string value, int caretP
4444
return Task.FromResult(default(CompletionSet));
4545
}
4646

47-
char separator = value.Contains('\\') ? '\\' : '/';
48-
int index = value.Length >= caretPosition - 1 ? value.LastIndexOf(separator, Math.Max(caretPosition - 1, 0)) : value.Length;
49-
string path = _provider.HostInteraction.WorkingDirectory;
50-
string prefix = "";
51-
52-
if (index > 0)
47+
try
5348
{
54-
prefix = value.Substring(0, index + 1);
55-
path = Path.Combine(path, prefix);
56-
}
49+
char separator = value.Contains('\\') ? '\\' : '/';
50+
int index = value.Length >= caretPosition - 1 ? value.LastIndexOf(separator, Math.Max(caretPosition - 1, 0)) : value.Length;
51+
string path = _provider.HostInteraction.WorkingDirectory;
52+
string prefix = "";
5753

58-
var set = new CompletionSet
59-
{
60-
Start = 0,
61-
Length = value.Length
62-
};
54+
if (index > 0)
55+
{
56+
prefix = value.Substring(0, index + 1);
57+
path = Path.Combine(path, prefix);
58+
}
6359

64-
var dir = new DirectoryInfo(path);
60+
var set = new CompletionSet
61+
{
62+
Start = 0,
63+
Length = value.Length
64+
};
6565

66-
if (dir.Exists)
67-
{
68-
var list = new List<CompletionItem>();
66+
var dir = new DirectoryInfo(path);
6967

70-
foreach (FileSystemInfo item in dir.EnumerateDirectories())
68+
if (dir.Exists)
7169
{
72-
var completion = new CompletionItem
70+
var list = new List<CompletionItem>();
71+
72+
foreach (FileSystemInfo item in dir.EnumerateDirectories())
7373
{
74-
DisplayText = item.Name + separator,
75-
InsertionText = prefix + item.Name + separator,
76-
};
74+
var completion = new CompletionItem
75+
{
76+
DisplayText = item.Name + separator,
77+
InsertionText = prefix + item.Name + separator,
78+
};
7779

78-
list.Add(completion);
79-
}
80+
list.Add(completion);
81+
}
8082

81-
foreach (FileSystemInfo item in dir.EnumerateFiles())
82-
{
83-
var completion = new CompletionItem
83+
foreach (FileSystemInfo item in dir.EnumerateFiles())
8484
{
85-
DisplayText = item.Name,
86-
InsertionText = prefix + item.Name,
87-
};
85+
var completion = new CompletionItem
86+
{
87+
DisplayText = item.Name,
88+
InsertionText = prefix + item.Name,
89+
};
90+
91+
list.Add(completion);
92+
}
8893

89-
list.Add(completion);
94+
set.Completions = list;
9095
}
9196

92-
set.Completions = list;
97+
return Task.FromResult(set);
98+
}
99+
catch
100+
{
101+
throw new InvalidLibraryException(value, _provider.Id);
93102
}
94-
95-
return Task.FromResult(set);
96103
}
97104

98105
/// <summary>
@@ -107,32 +114,39 @@ public async Task<ILibrary> GetLibraryAsync(string libraryId, CancellationToken
107114
{
108115
ILibrary library;
109116

110-
if (libraryId.Contains("://"))
117+
try
111118
{
119+
if (libraryId.Contains("://"))
120+
{
121+
library = new FileSystemLibrary
122+
{
123+
Name = libraryId,
124+
ProviderId = _provider.Id,
125+
Files = await GetFilesAsync(libraryId).ConfigureAwait(false)
126+
};
127+
128+
return library;
129+
}
130+
131+
string path = Path.Combine(_provider.HostInteraction.WorkingDirectory, libraryId);
132+
133+
if (!_underTest && !File.Exists(path) && !Directory.Exists(path))
134+
{
135+
return null;
136+
}
137+
112138
library = new FileSystemLibrary
113139
{
114140
Name = libraryId,
115141
ProviderId = _provider.Id,
116-
Files = await GetFilesAsync(libraryId).ConfigureAwait(false)
142+
Files = await GetFilesAsync(path).ConfigureAwait(false)
117143
};
118-
119-
return library;
120144
}
121-
122-
string path = Path.Combine(_provider.HostInteraction.WorkingDirectory, libraryId);
123-
124-
if (!_underTest && !File.Exists(path) && !Directory.Exists(path))
145+
catch (Exception)
125146
{
126-
return null;
147+
throw new InvalidLibraryException(libraryId, _provider.Id);
127148
}
128149

129-
library = new FileSystemLibrary
130-
{
131-
Name = libraryId,
132-
ProviderId = _provider.Id,
133-
Files = await GetFilesAsync(path).ConfigureAwait(false)
134-
};
135-
136150
return library;
137151
}
138152

src/LibraryManager/Providers/FileSystem/FileSystemProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public async Task<ILibraryOperationResult> UpdateStateAsync(ILibraryInstallation
144144

145145
if (library == null)
146146
{
147-
throw new InvalidLibraryException(desiredState.LibraryId, Id);
147+
return new LibraryOperationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId));
148148
}
149149

150150
if (desiredState.Files != null && desiredState.Files.Count > 0)
@@ -160,7 +160,7 @@ public async Task<ILibraryOperationResult> UpdateStateAsync(ILibraryInstallation
160160
Files = library.Files.Keys.ToList(),
161161
};
162162
}
163-
catch (Exception ex) when (ex is InvalidLibraryException || ex.InnerException is InvalidLibraryException)
163+
catch (InvalidLibraryException)
164164
{
165165
return new LibraryOperationResult(desiredState, PredefinedErrors.UnableToResolveSource(desiredState.LibraryId, desiredState.ProviderId));
166166
}

0 commit comments

Comments
 (0)