@@ -307,59 +307,131 @@ pub enum ErrorPosition {
307
307
} ,
308
308
}
309
309
310
- /// Reasons a new Postgres connection could fail.
310
+ #[ doc( hidden) ]
311
+ pub fn connect ( e : Box < error:: Error + Sync + Send > ) -> Error {
312
+ Error ( Box :: new ( ErrorKind :: ConnectParams ( e) ) )
313
+ }
314
+
315
+ #[ doc( hidden) ]
316
+ pub fn tls ( e : Box < error:: Error + Sync + Send > ) -> Error {
317
+ Error ( Box :: new ( ErrorKind :: Tls ( e) ) )
318
+ }
319
+
320
+ #[ doc( hidden) ]
321
+ pub fn db ( e : DbError ) -> Error {
322
+ Error ( Box :: new ( ErrorKind :: Db ( e) ) )
323
+ }
324
+
325
+ #[ doc( hidden) ]
326
+ pub fn io ( e : io:: Error ) -> Error {
327
+ Error ( Box :: new ( ErrorKind :: Io ( e) ) )
328
+ }
329
+
330
+ #[ doc( hidden) ]
331
+ pub fn conversion ( e : Box < error:: Error + Sync + Send > ) -> Error {
332
+ Error ( Box :: new ( ErrorKind :: Conversion ( e) ) )
333
+ }
334
+
311
335
#[ derive( Debug ) ]
312
- pub enum ConnectError {
313
- /// An error relating to connection parameters.
336
+ enum ErrorKind {
314
337
ConnectParams ( Box < error:: Error + Sync + Send > ) ,
315
- /// An error from the Postgres server itself.
316
- Db ( Box < DbError > ) ,
317
- /// An error initializing the TLS session.
318
338
Tls ( Box < error:: Error + Sync + Send > ) ,
319
- /// An error communicating with the server.
339
+ Db ( DbError ) ,
320
340
Io ( io:: Error ) ,
341
+ Conversion ( Box < error:: Error + Sync + Send > ) ,
321
342
}
322
343
323
- impl fmt:: Display for ConnectError {
344
+ /// An error communicating with the Postgres server.
345
+ #[ derive( Debug ) ]
346
+ pub struct Error ( Box < ErrorKind > ) ;
347
+
348
+ impl fmt:: Display for Error {
324
349
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
325
350
fmt. write_str ( error:: Error :: description ( self ) ) ?;
326
- match * self {
327
- ConnectError :: ConnectParams ( ref msg) => write ! ( fmt, ": {}" , msg) ,
328
- ConnectError :: Db ( ref err) => write ! ( fmt, ": {}" , err) ,
329
- ConnectError :: Tls ( ref err) => write ! ( fmt, ": {}" , err) ,
330
- ConnectError :: Io ( ref err) => write ! ( fmt, ": {}" , err) ,
351
+ match * self . 0 {
352
+ ErrorKind :: ConnectParams ( ref err) => write ! ( fmt, ": {}" , err) ,
353
+ ErrorKind :: Tls ( ref err) => write ! ( fmt, ": {}" , err) ,
354
+ ErrorKind :: Db ( ref err) => write ! ( fmt, ": {}" , err) ,
355
+ ErrorKind :: Io ( ref err) => write ! ( fmt, ": {}" , err) ,
356
+ ErrorKind :: Conversion ( ref err) => write ! ( fmt, ": {}" , err) ,
331
357
}
332
358
}
333
359
}
334
360
335
- impl error:: Error for ConnectError {
361
+ impl error:: Error for Error {
336
362
fn description ( & self ) -> & str {
337
- match * self {
338
- ConnectError :: ConnectParams ( _) => "Invalid connection parameters" ,
339
- ConnectError :: Db ( _) => "Error reported by Postgres" ,
340
- ConnectError :: Tls ( _) => "Error initiating SSL session" ,
341
- ConnectError :: Io ( _) => "Error communicating with the server" ,
363
+ match * self . 0 {
364
+ ErrorKind :: ConnectParams ( _) => "invalid connection parameters" ,
365
+ ErrorKind :: Tls ( _) => "TLS handshake error" ,
366
+ ErrorKind :: Db ( _) => "database error" ,
367
+ ErrorKind :: Io ( _) => "IO error" ,
368
+ ErrorKind :: Conversion ( _) => "type conversion error" ,
342
369
}
343
370
}
344
371
345
372
fn cause ( & self ) -> Option < & error:: Error > {
346
- match * self {
347
- ConnectError :: ConnectParams ( ref err) |
348
- ConnectError :: Tls ( ref err) => Some ( & * * err) ,
349
- ConnectError :: Db ( ref err) => Some ( & * * err) ,
350
- ConnectError :: Io ( ref err) => Some ( err) ,
373
+ match * self . 0 {
374
+ ErrorKind :: ConnectParams ( ref err) => Some ( & * * err) ,
375
+ ErrorKind :: Tls ( ref err) => Some ( & * * err) ,
376
+ ErrorKind :: Db ( ref err) => Some ( err) ,
377
+ ErrorKind :: Io ( ref err) => Some ( err) ,
378
+ ErrorKind :: Conversion ( ref err) => Some ( & * * err) ,
351
379
}
352
380
}
353
381
}
354
382
355
- impl From < io:: Error > for ConnectError {
356
- fn from ( err : io:: Error ) -> ConnectError {
357
- ConnectError :: Io ( err)
383
+ impl Error {
384
+ /// Returns the SQLSTATE error code associated with this error if it is a DB
385
+ /// error.
386
+ pub fn code ( & self ) -> Option < & SqlState > {
387
+ self . as_db ( ) . map ( |e| & e. code )
388
+ }
389
+
390
+ /// Returns the inner error if this is a connection parameter error.
391
+ pub fn as_connection ( & self ) -> Option < & ( error:: Error + ' static + Sync + Send ) > {
392
+ match * self . 0 {
393
+ ErrorKind :: ConnectParams ( ref err) => Some ( & * * err) ,
394
+ _ => None ,
395
+ }
396
+ }
397
+
398
+ /// Returns the `DbError` associated with this error if it is a DB error.
399
+ pub fn as_db ( & self ) -> Option < & DbError > {
400
+ match * self . 0 {
401
+ ErrorKind :: Db ( ref err) => Some ( err) ,
402
+ _ => None
403
+ }
404
+ }
405
+
406
+ /// Returns the inner error if this is a conversion error.
407
+ pub fn as_conversion ( & self ) -> Option < & ( error:: Error + ' static + Sync + Send ) > {
408
+ match * self . 0 {
409
+ ErrorKind :: Conversion ( ref err) => Some ( & * * err) ,
410
+ _ => None ,
411
+ }
412
+ }
413
+
414
+ /// Returns the inner `io::Error` associated with this error if it is an IO
415
+ /// error.
416
+ pub fn as_io ( & self ) -> Option < & io:: Error > {
417
+ match * self . 0 {
418
+ ErrorKind :: Io ( ref err) => Some ( err) ,
419
+ _ => None ,
420
+ }
358
421
}
359
422
}
360
423
361
- impl From < DbError > for ConnectError {
362
- fn from ( err : DbError ) -> ConnectError {
363
- ConnectError :: Db ( Box :: new ( err) )
424
+ impl From < io:: Error > for Error {
425
+ fn from ( err : io:: Error ) -> Error {
426
+ Error ( Box :: new ( ErrorKind :: Io ( err) ) )
427
+ }
428
+ }
429
+
430
+ impl From < Error > for io:: Error {
431
+ fn from ( err : Error ) -> io:: Error {
432
+ match * err. 0 {
433
+ ErrorKind :: Io ( e) => e,
434
+ _ => io:: Error :: new ( io:: ErrorKind :: Other , err) ,
435
+ }
364
436
}
365
437
}
0 commit comments