FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Dapper/Dapper.ProviderTools/DbExceptionExtensions.cs at main · DapperLib/Dapper · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
DapperLib
/
Dapper
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3.7k
Star
18.4k
Code
Issues
463
Pull requests
83
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Dapper
/
Dapper.ProviderTools
/
DbExceptionExtensions.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (56 loc) · 2.35 KB
Breadcrumbs
Dapper
/
Dapper.ProviderTools
/
DbExceptionExtensions.cs
Copy path
File metadata and controls
64 lines (56 loc) · 2.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using
System
;
using
System
.
Collections
.
Concurrent
;
using
System
.
Data
.
Common
;
using
System
.
Linq
.
Expressions
;
using
System
.
Reflection
;
namespace
Dapper
.
ProviderTools
{
/// <summary>
/// Helper utilities for working with database exceptions
/// </summary>
public
static
class
DbExceptionExtensions
{
/// <summary>
/// Indicates whether the provided exception has an integer Number property with the supplied value
/// </summary>
public
static
bool
IsNumber
(
this
DbException
exception
,
int
number
)
=>
exception
is
not
null
&&
ByTypeHelpers
.
Get
(
exception
.
GetType
(
)
)
.
IsNumber
(
exception
,
number
)
;
private
sealed
class
ByTypeHelpers
{
private
static
readonly
ConcurrentDictionary
<
Type
,
ByTypeHelpers
>
s_byType
=
new
ConcurrentDictionary
<
Type
,
ByTypeHelpers
>
(
)
;
private
readonly
Func
<
DbException
,
int
>
?
_getNumber
;
public
bool
IsNumber
(
DbException
exception
,
int
number
)
=>
_getNumber
is
not
null
&&
_getNumber
(
exception
)
==
number
;
public
static
ByTypeHelpers
Get
(
Type
type
)
{
if
(
!
s_byType
.
TryGetValue
(
type
,
out
var
value
)
)
{
s_byType
[
type
]
=
value
=
new
ByTypeHelpers
(
type
)
;
}
return
value
;
}
private
ByTypeHelpers
(
Type
type
)
{
_getNumber
=
TryGetInstanceProperty
<
int
>
(
"Number"
,
type
)
;
}
private
static
Func
<
DbException
,
T
>
?
TryGetInstanceProperty
<
T
>
(
string
name
,
Type
type
)
{
try
{
var
prop
=
type
.
GetProperty
(
name
,
BindingFlags
.
Public
|
BindingFlags
.
Instance
)
;
if
(
prop
is
null
||
!
prop
.
CanRead
)
return
null
;
if
(
prop
.
PropertyType
!=
typeof
(
T
)
)
return
null
;
var
p
=
Expression
.
Parameter
(
typeof
(
DbException
)
,
"exception"
)
;
var
body
=
Expression
.
Property
(
Expression
.
Convert
(
p
,
type
)
,
prop
)
;
var
lambda
=
Expression
.
Lambda
<
Func
<
DbException
,
T
>
>
(
body
,
p
)
;
return
lambda
.
Compile
(
)
;
}
catch
{
return
null
;
}
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL