@@ -83,6 +83,17 @@ impl Error {
8383 }
8484 }
8585
86+ #[ must_use]
87+ pub fn from_std_err ( code : Code , mut err : & dyn core:: error:: Error ) -> Self {
88+ let mut messages = vec ! [ format!( "{err}" ) ] ;
89+ while let Some ( src) = err. source ( ) {
90+ messages. push ( format ! ( "{src}" ) ) ;
91+ err = src;
92+ }
93+ messages. reverse ( ) ;
94+ Self :: new_with_messages ( code, messages)
95+ }
96+
8697 #[ inline]
8798 #[ must_use]
8899 pub fn append < S : Into < String > > ( mut self , msg : S ) -> Self {
@@ -162,37 +173,37 @@ impl core::fmt::Display for Error {
162173
163174impl From < prost:: DecodeError > for Error {
164175 fn from ( err : prost:: DecodeError ) -> Self {
165- make_err ! ( Code :: Internal , "{}" , err. to_string ( ) )
176+ Self :: from_std_err ( Code :: Internal , & err)
166177 }
167178}
168179
169180impl From < prost:: EncodeError > for Error {
170181 fn from ( err : prost:: EncodeError ) -> Self {
171- make_err ! ( Code :: Internal , "{}" , err. to_string ( ) )
182+ Self :: from_std_err ( Code :: Internal , & err)
172183 }
173184}
174185
175186impl From < prost:: UnknownEnumValue > for Error {
176187 fn from ( err : prost:: UnknownEnumValue ) -> Self {
177- make_err ! ( Code :: Internal , "{}" , err. to_string ( ) )
188+ Self :: from_std_err ( Code :: Internal , & err)
178189 }
179190}
180191
181192impl From < core:: num:: TryFromIntError > for Error {
182193 fn from ( err : core:: num:: TryFromIntError ) -> Self {
183- make_err ! ( Code :: InvalidArgument , "{}" , err. to_string ( ) )
194+ Self :: from_std_err ( Code :: InvalidArgument , & err)
184195 }
185196}
186197
187198impl From < tokio:: task:: JoinError > for Error {
188199 fn from ( err : tokio:: task:: JoinError ) -> Self {
189- make_err ! ( Code :: Internal , "{}" , err. to_string ( ) )
200+ Self :: from_std_err ( Code :: Internal , & err)
190201 }
191202}
192203
193204impl < T > From < PoisonError < MutexGuard < ' _ , T > > > for Error {
194205 fn from ( err : PoisonError < MutexGuard < ' _ , T > > ) -> Self {
195- make_err ! ( Code :: Internal , "{}" , err. to_string ( ) )
206+ Self :: from_std_err ( Code :: Internal , & err)
196207 }
197208}
198209
@@ -218,7 +229,7 @@ impl From<serde_json5::Error> for Error {
218229
219230impl From < core:: num:: ParseIntError > for Error {
220231 fn from ( err : core:: num:: ParseIntError ) -> Self {
221- make_err ! ( Code :: InvalidArgument , "{}" , err. to_string ( ) )
232+ Self :: from_std_err ( Code :: InvalidArgument , & err)
222233 }
223234}
224235
@@ -231,19 +242,19 @@ impl From<core::convert::Infallible> for Error {
231242
232243impl From < TimestampError > for Error {
233244 fn from ( err : TimestampError ) -> Self {
234- make_err ! ( Code :: InvalidArgument , "{}" , err)
245+ Self :: from_std_err ( Code :: InvalidArgument , & err)
235246 }
236247}
237248
238249impl From < AcquireError > for Error {
239250 fn from ( err : AcquireError ) -> Self {
240- make_err ! ( Code :: Internal , "{}" , err)
251+ Self :: from_std_err ( Code :: Internal , & err)
241252 }
242253}
243254
244255impl From < Utf8Error > for Error {
245256 fn from ( err : Utf8Error ) -> Self {
246- make_err ! ( Code :: Internal , "{}" , err)
257+ Self :: from_std_err ( Code :: Internal , & err)
247258 }
248259}
249260
@@ -295,32 +306,32 @@ impl From<Error> for tonic::Status {
295306}
296307
297308impl From < walkdir:: Error > for Error {
298- fn from ( value : walkdir:: Error ) -> Self {
299- Self :: new ( Code :: Internal , value . to_string ( ) )
309+ fn from ( err : walkdir:: Error ) -> Self {
310+ Self :: from_std_err ( Code :: Internal , & err )
300311 }
301312}
302313
303314impl From < uuid:: Error > for Error {
304- fn from ( value : uuid:: Error ) -> Self {
305- Self :: new ( Code :: Internal , value . to_string ( ) )
315+ fn from ( err : uuid:: Error ) -> Self {
316+ Self :: from_std_err ( Code :: Internal , & err )
306317 }
307318}
308319
309320impl From < rustls_pki_types:: pem:: Error > for Error {
310- fn from ( value : rustls_pki_types:: pem:: Error ) -> Self {
311- Self :: new ( Code :: Internal , value . to_string ( ) )
321+ fn from ( err : rustls_pki_types:: pem:: Error ) -> Self {
322+ Self :: from_std_err ( Code :: Internal , & err )
312323 }
313324}
314325
315326impl From < tokio:: time:: error:: Elapsed > for Error {
316- fn from ( value : tokio:: time:: error:: Elapsed ) -> Self {
317- Self :: new ( Code :: DeadlineExceeded , value . to_string ( ) )
327+ fn from ( err : tokio:: time:: error:: Elapsed ) -> Self {
328+ Self :: from_std_err ( Code :: DeadlineExceeded , & err )
318329 }
319330}
320331
321332impl From < url:: ParseError > for Error {
322- fn from ( value : url:: ParseError ) -> Self {
323- Self :: new ( Code :: Internal , value . to_string ( ) )
333+ fn from ( err : url:: ParseError ) -> Self {
334+ Self :: from_std_err ( Code :: DeadlineExceeded , & err )
324335 }
325336}
326337
0 commit comments