@@ -28,6 +28,56 @@ private sealed class CatalogUploadResult
28
28
public string AzureStorageId { get ; set ; }
29
29
}
30
30
31
+ /// <summary>
32
+ /// Artifact information returned from registering an artifact to the catalog.
33
+ /// </summary>
34
+ private sealed class CatalolRegisterResult
35
+ {
36
+ [ JsonProperty ( "catalogId" ) ]
37
+ public string CatalogId { get ; set ; }
38
+ }
39
+
40
+ /// <summary>
41
+ /// Represents an exception that is thrown when a version already exists.
42
+ /// </summary>
43
+ internal sealed class VersionAlreadyExistsException : Exception
44
+ {
45
+ /// <summary>
46
+ /// Gets the version that caused the exception.
47
+ /// </summary>
48
+ public string Version { get ; }
49
+
50
+ /// <summary>
51
+ /// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class.
52
+ /// </summary>
53
+ public VersionAlreadyExistsException ( )
54
+ : base ( "The specified version already exists." )
55
+ {
56
+ }
57
+
58
+ /// <summary>
59
+ /// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class
60
+ /// with a specified error message.
61
+ /// </summary>
62
+ /// <param name="version">The conflicting version.</param>
63
+ public VersionAlreadyExistsException ( string version )
64
+ : base ( $ "The specified version '{ version } ' already exists.")
65
+ {
66
+ Version = version ;
67
+ }
68
+
69
+ /// <summary>
70
+ /// Initializes a new instance of the <see cref="VersionAlreadyExistsException"/> class
71
+ /// with a specified error message and a reference to the inner exception that is the cause of this exception.
72
+ /// </summary>
73
+ /// <param name="version">The conflicting version.</param>
74
+ /// <param name="innerException">The exception that is the cause of the current exception.</param>
75
+ public VersionAlreadyExistsException ( string version , Exception innerException )
76
+ : base ( $ "The specified version '{ version } ' already exists.")
77
+ {
78
+ Version = version ;
79
+ }
80
+ }
31
81
32
82
private const string RegistrationPath = "api/key-catalog/v2-0/catalogs/register" ;
33
83
private const string VersionUploadPathEnd = "/register/version" ;
@@ -66,8 +116,8 @@ public async Task<ArtifactUploadResult> RegisterCatalogAsync(byte[] catalogDetai
66
116
67
117
if ( response . IsSuccessStatusCode )
68
118
{
69
- var returnedResult = JsonConvert . DeserializeObject < CatalogUploadResult > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
70
- return new ArtifactUploadResult ( ) { ArtifactId = returnedResult . AzureStorageId } ;
119
+ var returnedResult = JsonConvert . DeserializeObject < CatalolRegisterResult > ( await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ) ;
120
+ return new ArtifactUploadResult ( ) { ArtifactId = returnedResult . CatalogId } ;
71
121
}
72
122
73
123
if ( response . StatusCode is HttpStatusCode . Forbidden || response . StatusCode is HttpStatusCode . Unauthorized )
@@ -129,6 +179,11 @@ public async Task<ArtifactUploadResult> UploadVersionAsync(byte[] package, strin
129
179
throw new AuthenticationException ( $ "The version upload api returned a { response . StatusCode } response. Body: { body } ") ;
130
180
}
131
181
182
+ if ( response . StatusCode is HttpStatusCode . Conflict && body . Contains ( "already exists." ) )
183
+ {
184
+ throw new VersionAlreadyExistsException ( version ) ;
185
+ }
186
+
132
187
throw new InvalidOperationException ( $ "The version upload api returned a { response . StatusCode } response. Body: { body } ") ;
133
188
}
134
189
}
0 commit comments