@@ -238,28 +238,15 @@ def get_seedless_session(self) -> Session:
238
238
seedless_session = client .get_seedless_session ()
239
239
return seedless_session
240
240
241
- @contextmanager
242
- def client_context (self ):
243
- """Get a client instance as a context manager. Handle errors in a manner
244
- appropriate for end-users.
245
-
246
- Usage:
247
- >>> with obj.client_context() as client:
248
- >>> do_your_actions_here()
249
- """
241
+ def _connection_context (self , connect_fn : t .Callable [[], t .Any ]):
250
242
try :
251
- client = self .get_client ()
252
- except transport .DeviceIsBusy :
253
- click .echo ("Device is in use by another process." )
254
- sys .exit (1 )
255
- except Exception :
256
- click .echo ("Failed to find a Trezor device." )
257
- if self .path is not None :
258
- click .echo (f"Using path: { self .path } " )
243
+ conn = connect_fn ()
244
+ except Exception as e :
245
+ self ._print_exception (e , "Failed to connect" )
259
246
sys .exit (1 )
260
247
261
248
try :
262
- yield client
249
+ yield conn
263
250
except exceptions .Cancelled :
264
251
# handle cancel action
265
252
click .echo ("Action was cancelled." )
@@ -269,6 +256,17 @@ def client_context(self):
269
256
raise click .ClickException (str (e )) from e
270
257
# other exceptions may cause a traceback
271
258
259
+ @contextmanager
260
+ def client_context (self ):
261
+ """Get a client instance as a context manager. Handle errors in a manner
262
+ appropriate for end-users.
263
+
264
+ Usage:
265
+ >>> with obj.client_context() as client:
266
+ >>> do_your_actions_here()
267
+ """
268
+ yield from self ._connection_context (self .get_client )
269
+
272
270
@contextmanager
273
271
def session_context (
274
272
self ,
@@ -277,55 +275,25 @@ def session_context(
277
275
seedless : bool = False ,
278
276
must_resume : bool = False ,
279
277
):
280
- """Get a session instance as a context manager. Handle errors in a manner
281
- appropriate for end-users.
282
-
283
- Usage:
284
- >>> with obj.session_context() as session:
285
- >>> do_your_actions_here()
286
- """
287
- try :
288
- if seedless :
289
- session = self .get_seedless_session ()
290
- else :
291
- session = self .get_session (
292
- derive_cardano = derive_cardano ,
293
- empty_passphrase = empty_passphrase ,
294
- must_resume = must_resume ,
295
- )
296
- except exceptions .DeviceLocked :
297
- click .echo (
298
- "Device is locked, enter a pin on the device." ,
299
- err = True ,
278
+ yield from self ._connection_context (
279
+ self .get_seedless_session
280
+ if seedless
281
+ else lambda : self .get_session (
282
+ derive_cardano = derive_cardano ,
283
+ empty_passphrase = empty_passphrase ,
284
+ must_resume = must_resume ,
300
285
)
301
- sys .exit (1 )
302
- except transport .DeviceIsBusy :
303
- click .echo ("Device is in use by another process." )
304
- sys .exit (1 )
305
- except exceptions .UnexpectedCodeEntryTagException :
306
- click .echo ("Entered Code is invalid." )
307
- sys .exit (1 )
308
- except exceptions .FailedSessionResumption :
309
- sys .exit (1 )
310
- except exceptions .DerivationOnUninitaizedDeviceError :
311
- click .echo ("Device is not initialized." )
312
- sys .exit (1 )
313
- except Exception :
314
- click .echo ("Failed to find a Trezor device." )
315
- if self .path is not None :
316
- click .echo (f"Using path: { self .path } " )
317
- sys .exit (1 )
286
+ )
318
287
319
- try :
320
- yield session
321
- except exceptions .Cancelled :
322
- # handle cancel action
323
- click .echo ("Action was cancelled." )
324
- sys .exit (1 )
325
- except exceptions .TrezorException as e :
326
- # handle any Trezor-sent exceptions as user-readable
327
- raise click .ClickException (str (e )) from e
328
- # other exceptions may cause a traceback
288
+ def _print_exception (self , exc : Exception , message : str ):
289
+ LOG .debug (message , exc_info = True )
290
+ message = f"{ message } : { exc .__class__ .__name__ } "
291
+ if description := str (exc ):
292
+ message = f"{ message } ({ description } )"
293
+
294
+ click .echo (message )
295
+ if self .path is not None :
296
+ click .echo (f"Using path: { self .path } " )
329
297
330
298
331
299
def with_session (
0 commit comments