@@ -70,6 +70,8 @@ pub enum StoreError {
70
70
WriteFailure ( String , BlockNumber , String , String ) ,
71
71
#[ error( "database query timed out" ) ]
72
72
StatementTimeout ,
73
+ #[ error( "database constraint violated: {0}" ) ]
74
+ ConstraintViolation ( String ) ,
73
75
}
74
76
75
77
// Convenience to report an internal error
@@ -127,6 +129,7 @@ impl Clone for StoreError {
127
129
Self :: WriteFailure ( arg0. clone ( ) , arg1. clone ( ) , arg2. clone ( ) , arg3. clone ( ) )
128
130
}
129
131
Self :: StatementTimeout => Self :: StatementTimeout ,
132
+ Self :: ConstraintViolation ( arg0) => Self :: ConstraintViolation ( arg0. clone ( ) ) ,
130
133
}
131
134
}
132
135
}
@@ -135,6 +138,7 @@ impl StoreError {
135
138
pub fn from_diesel_error ( e : & DieselError ) -> Option < Self > {
136
139
const CONN_CLOSE : & str = "server closed the connection unexpectedly" ;
137
140
const STMT_TIMEOUT : & str = "canceling statement due to statement timeout" ;
141
+ const UNIQUE_CONSTR : & str = "duplicate key value violates unique constraint" ;
138
142
let DieselError :: DatabaseError ( _, info) = e else {
139
143
return None ;
140
144
} ;
@@ -146,6 +150,12 @@ impl StoreError {
146
150
Some ( StoreError :: DatabaseUnavailable )
147
151
} else if info. message ( ) . contains ( STMT_TIMEOUT ) {
148
152
Some ( StoreError :: StatementTimeout )
153
+ } else if info. message ( ) . contains ( UNIQUE_CONSTR ) {
154
+ let msg = match info. details ( ) {
155
+ Some ( details) => format ! ( "{}: {}" , info. message( ) , details. replace( '\n' , " " ) ) ,
156
+ None => info. message ( ) . to_string ( ) ,
157
+ } ;
158
+ Some ( StoreError :: ConstraintViolation ( msg) )
149
159
} else {
150
160
None
151
161
}
@@ -174,7 +184,8 @@ impl StoreError {
174
184
| UnknownTable ( _)
175
185
| UnknownAttribute ( _, _)
176
186
| InvalidIdentifier ( _)
177
- | UnsupportedFilter ( _, _) => true ,
187
+ | UnsupportedFilter ( _, _)
188
+ | ConstraintViolation ( _) => true ,
178
189
179
190
// non-deterministic errors
180
191
Unknown ( _)
0 commit comments