@@ -303,6 +303,16 @@ impl Error {
303303 self . code
304304 }
305305
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+
306316 /// Returns the extra description of this error, if any is available.
307317 pub fn extra_description ( & self ) -> Option < & str > {
308318 self . extra . as_ref ( ) . map ( |s| & * * s)
@@ -311,7 +321,7 @@ impl Error {
311321
312322impl fmt:: Display for Error {
313323 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
314- let desc = error :: Error :: description ( self ) ;
324+ let desc = self . description ( ) ;
315325 match self . extra {
316326 Some ( ref s) => write ! ( f, "[{}] {} ({})" , self . code( ) , desc, s) ,
317327 None => write ! ( f, "[{}] {}" , self . code( ) , desc) ,
@@ -322,22 +332,14 @@ impl fmt::Display for Error {
322332impl fmt:: Debug for Error {
323333 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
324334 f. debug_struct ( "Error" )
325- . field ( "description" , & error :: Error :: description ( self ) )
335+ . field ( "description" , & self . description ( ) )
326336 . field ( "code" , & self . code )
327337 . field ( "extra" , & self . extra )
328338 . finish ( )
329339 }
330340}
331341
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 { }
341343
342344/// An error returned from "share" operations.
343345///
@@ -382,11 +384,20 @@ impl ShareError {
382384 pub fn code ( & self ) -> curl_sys:: CURLSHcode {
383385 self . code
384386 }
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+ }
385396}
386397
387398impl fmt:: Display for ShareError {
388399 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
389- error :: Error :: description ( self ) . fmt ( f)
400+ self . description ( ) . fmt ( f)
390401 }
391402}
392403
@@ -395,21 +406,13 @@ impl fmt::Debug for ShareError {
395406 write ! (
396407 f,
397408 "ShareError {{ description: {:?}, code: {} }}" ,
398- error :: Error :: description( self ) ,
409+ self . description( ) ,
399410 self . code
400411 )
401412 }
402413}
403414
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 { }
413416
414417/// An error from "multi" operations.
415418///
@@ -469,34 +472,33 @@ impl MultiError {
469472 pub fn code ( & self ) -> curl_sys:: CURLMcode {
470473 self . code
471474 }
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+ }
472484}
473485
474486impl fmt:: Display for MultiError {
475487 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
476- error :: Error :: description ( self ) . fmt ( f)
488+ self . description ( ) . fmt ( f)
477489 }
478490}
479491
480492impl fmt:: Debug for MultiError {
481493 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 ( )
488498 }
489499}
490500
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 { }
500502
501503/// An error from "form add" operations.
502504///
@@ -551,27 +553,9 @@ impl FormError {
551553 pub fn code ( & self ) -> curl_sys:: CURLFORMcode {
552554 self . code
553555 }
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- }
572556
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 {
575559 match self . code {
576560 curl_sys:: CURL_FORMADD_MEMORY => "allocation failure" ,
577561 curl_sys:: CURL_FORMADD_OPTION_TWICE => "one option passed twice" ,
@@ -587,6 +571,23 @@ impl error::Error for FormError {
587571 }
588572}
589573
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+
590591impl From < ffi:: NulError > for Error {
591592 fn from ( _: ffi:: NulError ) -> Error {
592593 Error {
0 commit comments