@@ -303,6 +303,16 @@ impl Error {
303
303
self . code
304
304
}
305
305
306
+ /// Returns the general description of this error code, using curl's
307
+ /// builtin `strerror`-like functionality.
308
+ pub fn description ( & self ) -> & str {
309
+ unsafe {
310
+ let s = curl_sys:: curl_easy_strerror ( self . code ) ;
311
+ assert ! ( !s. is_null( ) ) ;
312
+ str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
313
+ }
314
+ }
315
+
306
316
/// Returns the extra description of this error, if any is available.
307
317
pub fn extra_description ( & self ) -> Option < & str > {
308
318
self . extra . as_ref ( ) . map ( |s| & * * s)
@@ -311,7 +321,7 @@ impl Error {
311
321
312
322
impl fmt:: Display for Error {
313
323
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
314
- let desc = error :: Error :: description ( self ) ;
324
+ let desc = self . description ( ) ;
315
325
match self . extra {
316
326
Some ( ref s) => write ! ( f, "[{}] {} ({})" , self . code( ) , desc, s) ,
317
327
None => write ! ( f, "[{}] {}" , self . code( ) , desc) ,
@@ -322,22 +332,14 @@ impl fmt::Display for Error {
322
332
impl fmt:: Debug for Error {
323
333
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
324
334
f. debug_struct ( "Error" )
325
- . field ( "description" , & error :: Error :: description ( self ) )
335
+ . field ( "description" , & self . description ( ) )
326
336
. field ( "code" , & self . code )
327
337
. field ( "extra" , & self . extra )
328
338
. finish ( )
329
339
}
330
340
}
331
341
332
- impl error:: Error for Error {
333
- fn description ( & self ) -> & str {
334
- unsafe {
335
- let s = curl_sys:: curl_easy_strerror ( self . code ) ;
336
- assert ! ( !s. is_null( ) ) ;
337
- str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
338
- }
339
- }
340
- }
342
+ impl error:: Error for Error { }
341
343
342
344
/// An error returned from "share" operations.
343
345
///
@@ -382,11 +384,20 @@ impl ShareError {
382
384
pub fn code ( & self ) -> curl_sys:: CURLSHcode {
383
385
self . code
384
386
}
387
+
388
+ /// Returns curl's human-readable version of this error.
389
+ pub fn description ( & self ) -> & str {
390
+ unsafe {
391
+ let s = curl_sys:: curl_share_strerror ( self . code ) ;
392
+ assert ! ( !s. is_null( ) ) ;
393
+ str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
394
+ }
395
+ }
385
396
}
386
397
387
398
impl fmt:: Display for ShareError {
388
399
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
389
- error :: Error :: description ( self ) . fmt ( f)
400
+ self . description ( ) . fmt ( f)
390
401
}
391
402
}
392
403
@@ -395,21 +406,13 @@ impl fmt::Debug for ShareError {
395
406
write ! (
396
407
f,
397
408
"ShareError {{ description: {:?}, code: {} }}" ,
398
- error :: Error :: description( self ) ,
409
+ self . description( ) ,
399
410
self . code
400
411
)
401
412
}
402
413
}
403
414
404
- impl error:: Error for ShareError {
405
- fn description ( & self ) -> & str {
406
- unsafe {
407
- let s = curl_sys:: curl_share_strerror ( self . code ) ;
408
- assert ! ( !s. is_null( ) ) ;
409
- str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
410
- }
411
- }
412
- }
415
+ impl error:: Error for ShareError { }
413
416
414
417
/// An error from "multi" operations.
415
418
///
@@ -469,34 +472,33 @@ impl MultiError {
469
472
pub fn code ( & self ) -> curl_sys:: CURLMcode {
470
473
self . code
471
474
}
475
+
476
+ /// Returns curl's human-readable description of this error.
477
+ pub fn description ( & self ) -> & str {
478
+ unsafe {
479
+ let s = curl_sys:: curl_multi_strerror ( self . code ) ;
480
+ assert ! ( !s. is_null( ) ) ;
481
+ str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
482
+ }
483
+ }
472
484
}
473
485
474
486
impl fmt:: Display for MultiError {
475
487
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
476
- error :: Error :: description ( self ) . fmt ( f)
488
+ self . description ( ) . fmt ( f)
477
489
}
478
490
}
479
491
480
492
impl fmt:: Debug for MultiError {
481
493
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
482
- write ! (
483
- f,
484
- "MultiError {{ description: {:?}, code: {} }}" ,
485
- error:: Error :: description( self ) ,
486
- self . code
487
- )
494
+ f. debug_struct ( "MultiError" )
495
+ . field ( "description" , & self . description ( ) )
496
+ . field ( "code" , & self . code )
497
+ . finish ( )
488
498
}
489
499
}
490
500
491
- impl error:: Error for MultiError {
492
- fn description ( & self ) -> & str {
493
- unsafe {
494
- let s = curl_sys:: curl_multi_strerror ( self . code ) ;
495
- assert ! ( !s. is_null( ) ) ;
496
- str:: from_utf8 ( CStr :: from_ptr ( s) . to_bytes ( ) ) . unwrap ( )
497
- }
498
- }
499
- }
501
+ impl error:: Error for MultiError { }
500
502
501
503
/// An error from "form add" operations.
502
504
///
@@ -551,27 +553,9 @@ impl FormError {
551
553
pub fn code ( & self ) -> curl_sys:: CURLFORMcode {
552
554
self . code
553
555
}
554
- }
555
-
556
- impl fmt:: Display for FormError {
557
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
558
- error:: Error :: description ( self ) . fmt ( f)
559
- }
560
- }
561
-
562
- impl fmt:: Debug for FormError {
563
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
564
- write ! (
565
- f,
566
- "FormError {{ description: {:?}, code: {} }}" ,
567
- error:: Error :: description( self ) ,
568
- self . code
569
- )
570
- }
571
- }
572
556
573
- impl error :: Error for FormError {
574
- fn description ( & self ) -> & str {
557
+ /// Returns a human-readable description of this error code.
558
+ pub fn description ( & self ) -> & str {
575
559
match self . code {
576
560
curl_sys:: CURL_FORMADD_MEMORY => "allocation failure" ,
577
561
curl_sys:: CURL_FORMADD_OPTION_TWICE => "one option passed twice" ,
@@ -587,6 +571,23 @@ impl error::Error for FormError {
587
571
}
588
572
}
589
573
574
+ impl fmt:: Display for FormError {
575
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
576
+ self . description ( ) . fmt ( f)
577
+ }
578
+ }
579
+
580
+ impl fmt:: Debug for FormError {
581
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
582
+ f. debug_struct ( "FormError" )
583
+ . field ( "description" , & self . description ( ) )
584
+ . field ( "code" , & self . code )
585
+ . finish ( )
586
+ }
587
+ }
588
+
589
+ impl error:: Error for FormError { }
590
+
590
591
impl From < ffi:: NulError > for Error {
591
592
fn from ( _: ffi:: NulError ) -> Error {
592
593
Error {
0 commit comments