@@ -6,10 +6,8 @@ use std::error;
6
6
use std:: convert:: From ;
7
7
use std:: fmt;
8
8
use std:: io;
9
- use std:: result;
10
9
11
10
pub use self :: sqlstate:: SqlState ;
12
- use { Result , DbErrorNew } ;
13
11
14
12
mod sqlstate;
15
13
@@ -141,8 +139,9 @@ pub struct DbError {
141
139
_p : ( ) ,
142
140
}
143
141
144
- impl DbErrorNew for DbError {
145
- fn new_raw ( fields : & mut ErrorFields ) -> io:: Result < DbError > {
142
+ impl DbError {
143
+ #[ doc( hidden) ]
144
+ pub fn new_raw ( fields : & mut ErrorFields ) -> io:: Result < DbError > {
146
145
let mut severity = None ;
147
146
let mut parsed_severity = None ;
148
147
let mut code = None ;
@@ -169,8 +168,18 @@ impl DbErrorNew for DbError {
169
168
b'M' => message = Some ( field. value ( ) . to_owned ( ) ) ,
170
169
b'D' => detail = Some ( field. value ( ) . to_owned ( ) ) ,
171
170
b'H' => hint = Some ( field. value ( ) . to_owned ( ) ) ,
172
- b'P' => normal_position = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| :: bad_response ( ) ) ) ) ,
173
- b'p' => internal_position = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| :: bad_response ( ) ) ) ) ,
171
+ b'P' => {
172
+ normal_position = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| {
173
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
174
+ "`P` field did not contain an integer" )
175
+ } ) ) ) ;
176
+ }
177
+ b'p' => {
178
+ internal_position = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| {
179
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
180
+ "`p` field did not contain an integer" )
181
+ } ) ) ) ;
182
+ }
174
183
b'q' => internal_query = Some ( field. value ( ) . to_owned ( ) ) ,
175
184
b'W' => where_ = Some ( field. value ( ) . to_owned ( ) ) ,
176
185
b's' => schema = Some ( field. value ( ) . to_owned ( ) ) ,
@@ -179,18 +188,32 @@ impl DbErrorNew for DbError {
179
188
b'd' => datatype = Some ( field. value ( ) . to_owned ( ) ) ,
180
189
b'n' => constraint = Some ( field. value ( ) . to_owned ( ) ) ,
181
190
b'F' => file = Some ( field. value ( ) . to_owned ( ) ) ,
182
- b'L' => line = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| :: bad_response ( ) ) ) ) ,
191
+ b'L' => {
192
+ line = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| {
193
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
194
+ "`L` field did not contain an integer" )
195
+ } ) ) ) ;
196
+ }
183
197
b'R' => routine = Some ( field. value ( ) . to_owned ( ) ) ,
184
- b'V' => parsed_severity = Some ( try!( Severity :: from_str ( field. value ( ) ) . ok_or_else ( :: bad_response) ) ) ,
198
+ b'V' => {
199
+ parsed_severity = Some ( try!( Severity :: from_str ( field. value ( ) ) . ok_or_else ( || {
200
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
201
+ "`V` field contained an invalid value" )
202
+ } ) ) ) ;
203
+ }
185
204
_ => { } ,
186
205
}
187
206
}
188
207
189
208
Ok ( DbError {
190
- severity : try!( severity. ok_or_else ( || :: bad_response ( ) ) ) ,
209
+ severity : try!( severity. ok_or_else ( || {
210
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput , "`S` field missing" )
211
+ } ) ) ,
191
212
parsed_severity : parsed_severity,
192
- code : try!( code. ok_or_else ( || :: bad_response ( ) ) ) ,
193
- message : try!( message. ok_or_else ( || :: bad_response ( ) ) ) ,
213
+ code : try!( code. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
214
+ "`C` field missing" ) ) ) ,
215
+ message : try!( message. ok_or_else ( || io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
216
+ "`M` field missing" ) ) ) ,
194
217
detail : detail,
195
218
hint : hint,
196
219
position : match normal_position {
@@ -200,7 +223,10 @@ impl DbErrorNew for DbError {
200
223
Some ( position) => {
201
224
Some ( ErrorPosition :: Internal {
202
225
position : position,
203
- query : try!( internal_query. ok_or_else ( || :: bad_response ( ) ) ) ,
226
+ query : try!( internal_query. ok_or_else ( || {
227
+ io:: Error :: new ( io:: ErrorKind :: InvalidInput ,
228
+ "`q` field missing but `p` field present" )
229
+ } ) ) ,
204
230
} )
205
231
}
206
232
None => None ,
@@ -220,14 +246,16 @@ impl DbErrorNew for DbError {
220
246
} )
221
247
}
222
248
223
- fn new_connect < T > ( fields : & mut ErrorFields ) -> result:: Result < T , ConnectError > {
249
+ #[ doc( hidden) ]
250
+ pub fn new_connect < T > ( fields : & mut ErrorFields ) -> Result < T , ConnectError > {
224
251
match DbError :: new_raw ( fields) {
225
252
Ok ( err) => Err ( ConnectError :: Db ( Box :: new ( err) ) ) ,
226
253
Err ( e) => Err ( ConnectError :: Io ( e) ) ,
227
254
}
228
255
}
229
256
230
- fn new < T > ( fields : & mut ErrorFields ) -> Result < T > {
257
+ #[ doc( hidden) ]
258
+ pub fn new < T > ( fields : & mut ErrorFields ) -> Result < T , Error > {
231
259
match DbError :: new_raw ( fields) {
232
260
Ok ( err) => Err ( Error :: Db ( Box :: new ( err) ) ) ,
233
261
Err ( e) => Err ( Error :: Io ( e) ) ,
0 commit comments