-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unique constraint check to extension method (#10216)
- Loading branch information
1 parent
61e5c18
commit 099427b
Showing
2 changed files
with
37 additions
and
15 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/NuGet.Services.Entities/Extensions/ExceptionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace NuGet.Services.Entities | ||
{ | ||
public static class ExceptionExtensions | ||
{ | ||
public static bool IsSqlUniqueConstraintViolation(this DataException exception) | ||
{ | ||
Exception current = exception; | ||
while (current is not null) | ||
{ | ||
if (current is SqlException sqlException) | ||
{ | ||
switch (sqlException.Number) | ||
{ | ||
case 547: // Constraint check violation: https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-0-to-999 | ||
case 2601: // Duplicated key row error: https://learn.microsoft.com/en-us/sql/relational-databases/replication/mssql-eng002601 | ||
case 2627: // Unique constraint error: https://learn.microsoft.com/en-us/sql/relational-databases/replication/mssql-eng002627 | ||
return true; | ||
} | ||
} | ||
|
||
current = current.InnerException; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters