Skip to content

Commit ba88565

Browse files
committed
improve exception handling
1 parent 7786f63 commit ba88565

16 files changed

+346
-291
lines changed

FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
<PropertyGroup>
1414
<AssemblyName>SharpGrip.FileSystem.Adapters.AmazonS3</AssemblyName>
1515
<PackageId>SharpGrip.FileSystem.Adapters.AmazonS3</PackageId>
16-
<PackageVersion>1.0.0-alpha1</PackageVersion>
16+
<PackageVersion>1.0.0-alpha2</PackageVersion>
1717
<Title>SharpGrip FileSystem AmazonS3 adapter</Title>
1818
<Description>The SharpGrip FileSystem AmazonS3 adapter.</Description>
1919
<Company>SharpGrip</Company>
2020
<Authors>SharpGrip</Authors>
2121
<Copyright>SharpGrip</Copyright>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
23+
<PackageTags>sharpgrip;file-system;amazon-s3</PackageTags>
2324
<PackageProjectUrl>https://sharpgrip.net</PackageProjectUrl>
2425
<RepositoryUrl>https://github.com/SharpGrip/FileSystem</RepositoryUrl>
2526
<RepositoryType>git</RepositoryType>

FileSystem.Adapters.AmazonS3/src/AmazonS3Adapter.cs

Lines changed: 51 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,11 @@ public override async Task<IFile> GetFileAsync(string path, CancellationToken ca
5050
throw new FileNotFoundException(path, Prefix);
5151
}
5252

53-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
54-
{
55-
throw new ConnectionException(exception);
56-
}
57-
58-
throw new AdapterRuntimeException(exception);
53+
throw Exception(exception);
5954
}
6055
catch (Exception exception)
6156
{
62-
throw new AdapterRuntimeException(exception);
57+
throw Exception(exception);
6358
}
6459
}
6560

@@ -100,27 +95,15 @@ public override async Task<IDirectory> GetDirectoryAsync(string path, Cancellati
10095

10196
throw new DirectoryNotFoundException(path, Prefix);
10297
}
103-
catch (FileSystemException)
104-
{
105-
throw;
106-
}
107-
catch (AmazonS3Exception exception)
108-
{
109-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
110-
{
111-
throw new ConnectionException(exception);
112-
}
113-
114-
throw new AdapterRuntimeException(exception);
115-
}
11698
catch (Exception exception)
11799
{
118-
throw new AdapterRuntimeException(exception);
100+
throw Exception(exception);
119101
}
120102
}
121103

122104
public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", CancellationToken cancellationToken = default)
123105
{
106+
await GetDirectoryAsync(path, cancellationToken);
124107
path = PrependRootPath(path);
125108

126109
try
@@ -142,18 +125,9 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", C
142125

143126
return files;
144127
}
145-
catch (AmazonS3Exception exception)
146-
{
147-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
148-
{
149-
throw new ConnectionException(exception);
150-
}
151-
152-
throw new AdapterRuntimeException(exception);
153-
}
154128
catch (Exception exception)
155129
{
156-
throw new AdapterRuntimeException(exception);
130+
throw Exception(exception);
157131
}
158132
}
159133

@@ -162,6 +136,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
162136
CancellationToken cancellationToken = default
163137
)
164138
{
139+
await GetDirectoryAsync(path, cancellationToken);
165140
path = PrependRootPath(path);
166141

167142
try
@@ -183,56 +158,39 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
183158

184159
return directories;
185160
}
186-
catch (AmazonS3Exception exception)
187-
{
188-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
189-
{
190-
throw new ConnectionException(exception);
191-
}
192-
193-
throw new AdapterRuntimeException(exception);
194-
}
195161
catch (Exception exception)
196162
{
197-
throw new AdapterRuntimeException(exception);
163+
throw Exception(exception);
198164
}
199165
}
200166

201167
public override async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
202168
{
203-
if (!path.EndsWith('/'))
169+
if (await DirectoryExistsAsync(path, cancellationToken))
204170
{
205-
path += "/";
171+
throw new DirectoryExistsException(PrependRootPath(path), Prefix);
206172
}
207173

174+
path = PrependRootPath(path);
175+
path = path.EndsWith('/') ? path : path + "/";
176+
208177
try
209178
{
210179
var request = new PutObjectRequest {BucketName = bucketName, Key = path, InputStream = new MemoryStream()};
211180
await client.PutObjectAsync(request, cancellationToken);
212181
}
213-
catch (AmazonS3Exception exception)
214-
{
215-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
216-
{
217-
throw new ConnectionException(exception);
218-
}
219-
220-
throw new AdapterRuntimeException(exception);
221-
}
222182
catch (Exception exception)
223183
{
224-
throw new AdapterRuntimeException(exception);
184+
throw Exception(exception);
225185
}
226186
}
227187

228188
public override async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
229189
{
230190
await GetDirectoryAsync(path, cancellationToken);
231191

232-
if (!path.EndsWith('/'))
233-
{
234-
path += "/";
235-
}
192+
path = PrependRootPath(path);
193+
path = path.EndsWith('/') ? path : path + "/";
236194

237195
try
238196
{
@@ -248,47 +206,31 @@ public override async Task DeleteDirectoryAsync(string path, CancellationToken c
248206

249207
await client.DeleteObjectsAsync(deleteObjectsRequest, cancellationToken);
250208
}
251-
catch (AmazonS3Exception exception)
252-
{
253-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
254-
{
255-
throw new ConnectionException(exception);
256-
}
257-
258-
throw new AdapterRuntimeException(exception);
259-
}
260209
catch (Exception exception)
261210
{
262-
throw new AdapterRuntimeException(exception);
211+
throw Exception(exception);
263212
}
264213
}
265214

266215
public override async Task DeleteFileAsync(string path, CancellationToken cancellationToken = default)
267216
{
268217
await GetFileAsync(path, cancellationToken);
218+
path = PrependRootPath(path);
269219

270220
try
271221
{
272222
await client.DeleteObjectAsync(bucketName, path, cancellationToken);
273223
}
274-
catch (AmazonS3Exception exception)
275-
{
276-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
277-
{
278-
throw new ConnectionException(exception);
279-
}
280-
281-
throw new AdapterRuntimeException(exception);
282-
}
283224
catch (Exception exception)
284225
{
285-
throw new AdapterRuntimeException(exception);
226+
throw Exception(exception);
286227
}
287228
}
288229

289230
public override async Task<byte[]> ReadFileAsync(string path, CancellationToken cancellationToken = default)
290231
{
291232
await GetFileAsync(path, cancellationToken);
233+
path = PrependRootPath(path);
292234

293235
try
294236
{
@@ -299,24 +241,16 @@ public override async Task<byte[]> ReadFileAsync(string path, CancellationToken
299241

300242
return memoryStream.ToArray();
301243
}
302-
catch (AmazonS3Exception exception)
303-
{
304-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
305-
{
306-
throw new ConnectionException(exception);
307-
}
308-
309-
throw new AdapterRuntimeException(exception);
310-
}
311244
catch (Exception exception)
312245
{
313-
throw new AdapterRuntimeException(exception);
246+
throw Exception(exception);
314247
}
315248
}
316249

317250
public override async Task<string> ReadTextFileAsync(string path, CancellationToken cancellationToken = default)
318251
{
319252
await GetFileAsync(path, cancellationToken);
253+
path = PrependRootPath(path);
320254

321255
try
322256
{
@@ -330,29 +264,26 @@ public override async Task<string> ReadTextFileAsync(string path, CancellationTo
330264

331265
return await streamReader.ReadToEndAsync();
332266
}
333-
catch (AmazonS3Exception exception)
334-
{
335-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
336-
{
337-
throw new ConnectionException(exception);
338-
}
339-
340-
throw new AdapterRuntimeException(exception);
341-
}
342267
catch (Exception exception)
343268
{
344-
throw new AdapterRuntimeException(exception);
269+
throw Exception(exception);
345270
}
346271
}
347272

348-
public override async Task WriteFileAsync(string path, byte[] contents, bool overwrite = false,
349-
CancellationToken cancellationToken = default)
273+
public override async Task WriteFileAsync(
274+
string path,
275+
byte[] contents,
276+
bool overwrite = false,
277+
CancellationToken cancellationToken = default
278+
)
350279
{
351280
if (!overwrite && await FileExistsAsync(path, cancellationToken))
352281
{
353282
throw new FileExistsException(PrependRootPath(path), Prefix);
354283
}
355284

285+
path = PrependRootPath(path);
286+
356287
try
357288
{
358289
await using var memoryStream = new MemoryStream(contents);
@@ -365,18 +296,9 @@ public override async Task WriteFileAsync(string path, byte[] contents, bool ove
365296

366297
await client.PutObjectAsync(request, cancellationToken);
367298
}
368-
catch (AmazonS3Exception exception)
369-
{
370-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
371-
{
372-
throw new ConnectionException(exception);
373-
}
374-
375-
throw new AdapterRuntimeException(exception);
376-
}
377299
catch (Exception exception)
378300
{
379-
throw new AdapterRuntimeException(exception);
301+
throw Exception(exception);
380302
}
381303
}
382304

@@ -387,6 +309,8 @@ public override async Task AppendFileAsync(string path, byte[] contents, Cancell
387309
contents = existingContents.Concat(contents).ToArray();
388310
await DeleteFileAsync(path, cancellationToken);
389311

312+
path = PrependRootPath(path);
313+
390314
try
391315
{
392316
await using var memoryStream = new MemoryStream(contents);
@@ -399,19 +323,28 @@ public override async Task AppendFileAsync(string path, byte[] contents, Cancell
399323

400324
await client.PutObjectAsync(request, cancellationToken);
401325
}
402-
catch (AmazonS3Exception exception)
326+
catch (Exception exception)
403327
{
404-
if (exception.ErrorCode == "InvalidAccessKeyId" || exception.ErrorCode == "InvalidSecurity")
405-
{
406-
throw new ConnectionException(exception);
407-
}
408-
409-
throw new AdapterRuntimeException(exception);
328+
throw Exception(exception);
410329
}
411-
catch (Exception exception)
330+
}
331+
332+
private static Exception Exception(Exception exception)
333+
{
334+
if (exception is FileSystemException)
412335
{
413-
throw new AdapterRuntimeException(exception);
336+
return exception;
414337
}
338+
339+
if (exception is AmazonS3Exception amazonS3Exception)
340+
{
341+
if (amazonS3Exception.ErrorCode == "InvalidAccessKeyId" || amazonS3Exception.ErrorCode == "InvalidSecurity")
342+
{
343+
return new ConnectionException(exception);
344+
}
345+
}
346+
347+
return new AdapterRuntimeException(exception);
415348
}
416349
}
417350
}

FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Azure.Storage.Blobs" Version="12.4.2" />
30+
<PackageReference Include="Azure.Storage.Blobs" Version="12.4.2"/>
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<ProjectReference Include="..\FileSystem\FileSystem.csproj" />
34+
<ProjectReference Include="..\FileSystem\FileSystem.csproj"/>
3535
</ItemGroup>
3636

3737
</Project>

FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
<PropertyGroup>
1414
<AssemblyName>SharpGrip.FileSystem.Adapters.AzureFileStorage</AssemblyName>
1515
<PackageId>SharpGrip.FileSystem.Adapters.AzureFileStorage</PackageId>
16-
<PackageVersion>1.0.0-alpha1</PackageVersion>
16+
<PackageVersion>1.0.0-alpha2</PackageVersion>
1717
<Title>SharpGrip FileSystem AzureFileStorage adapter</Title>
1818
<Description>The SharpGrip FileSystem AzureFileStorage adapter.</Description>
1919
<Company>SharpGrip</Company>
2020
<Authors>SharpGrip</Authors>
2121
<Copyright>SharpGrip</Copyright>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
23+
<PackageTags>sharpgrip;file-system;azure-file-storage</PackageTags>
2324
<PackageProjectUrl>https://sharpgrip.net</PackageProjectUrl>
2425
<RepositoryUrl>https://github.com/SharpGrip/FileSystem</RepositoryUrl>
2526
<RepositoryType>git</RepositoryType>

0 commit comments

Comments
 (0)