2
2
// See the COPYRIGHT file at the top-level directory of this distribution.
3
3
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
4
5
+ use std:: str;
6
+ use std:: ffi:: CStr ;
7
+ use std:: fmt:: { self , Formatter , Debug , Display } ;
8
+ use std:: error;
5
9
use ffi:: { self , GQuark } ;
6
10
use glib_container:: GlibContainer ;
7
11
use translate:: ToGlibPtr ;
@@ -37,7 +41,7 @@ impl Error {
37
41
38
42
/// Returns true if error matches domain and code , false otherwise. In particular, when error.pointer
39
43
/// is NULL, false will be returned.
40
- ///
44
+ ///
41
45
/// If domain contains a FAILED (or otherwise generic) error code, you should generally not check
42
46
/// for it explicitly, but should instead treat any not-explicitly-recognized error code as being
43
47
/// equivalent to the FAILED code. This way, if the domain is extended in the future to provide a
@@ -59,12 +63,18 @@ impl Error {
59
63
60
64
/// If other.pointer is NULL, free self ; otherwise, moves self into other. The error variable
61
65
/// other.pointer points to must be NULL.
62
- ///
66
+ ///
63
67
/// Note that self is no longer valid after this call. If you want to keep using the same Error
64
68
/// struct, you need to set it to NULL after calling this function on it.
65
69
pub fn propagate ( & mut self , other : & Error ) -> ( ) {
66
70
unsafe { ffi:: g_propagate_error ( & mut self . pointer , other. pointer ) }
67
71
}
72
+
73
+ /// Returns the error message stored in the wrapped GError
74
+ pub fn message ( & self ) -> & str {
75
+ let c_str = unsafe { CStr :: from_ptr ( ( * self . pointer ) . message ) } ;
76
+ str:: from_utf8 ( c_str. to_bytes ( ) ) . unwrap ( )
77
+ }
68
78
}
69
79
70
80
impl Clone for Error {
@@ -81,6 +91,24 @@ impl Clone for Error {
81
91
}
82
92
}
83
93
94
+ impl Debug for Error {
95
+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
96
+ write ! ( f, "{}" , self . message( ) )
97
+ }
98
+ }
99
+
100
+ impl Display for Error {
101
+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
102
+ write ! ( f, "{}" , self . message( ) )
103
+ }
104
+ }
105
+
106
+ impl error:: Error for Error {
107
+ fn description ( & self ) -> & str {
108
+ self . message ( )
109
+ }
110
+ }
111
+
84
112
impl Drop for Error {
85
113
fn drop ( & mut self ) {
86
114
self . release ( ) ;
0 commit comments