(*args, **kwargs)
| 440 | |
| 441 | @wraps(fn) |
| 442 | def wrapped(*args, **kwargs): |
| 443 | # text/json output taken from callee args |
| 444 | json_output = kwargs.get('json_output', False) |
| 445 | raw_output = kwargs.get('raw_output', False) |
| 446 | |
| 447 | now = utcnow() |
| 448 | info = dict(datetime=now.isoformat(), timestamp=datetime_to_timestamp(now), code=0) |
| 449 | |
| 450 | def output(fmt=None, maxlen=None, **params): |
| 451 | raw = params.pop('raw', None) |
| 452 | if raw_output: |
| 453 | if raw is not None: |
| 454 | click.echo(raw) |
| 455 | elif json_output: |
| 456 | info.update(params) |
| 457 | else: |
| 458 | if fmt is not None: |
| 459 | msg = fmt.format(**params) |
| 460 | else: |
| 461 | # logfmt-like output |
| 462 | msg = ' '.join(f'{k}={v}' for k,v in params.items()) |
| 463 | if maxlen is not None: |
| 464 | msg = strtrunc(msg, maxlen) |
| 465 | click.echo(msg) |
| 466 | |
| 467 | def flush(): |
| 468 | if json_output and not raw_output: |
| 469 | click.echo(orjson.dumps(info)) |
| 470 | |
| 471 | try: |
| 472 | fn(*args, output=output, **kwargs) |
| 473 | except CLIError as error: |
| 474 | output("Error: {error} (code: {code})", error=str(error), code=error.code) |
| 475 | sys.exit(error.code) |
| 476 | except OAuthError as error: |
| 477 | output("Auth error: {error}", error=str(error)) |
| 478 | output('Re-authorize with "dwave auth login" and retry.') |
| 479 | sys.exit(127) |
| 480 | except Exception as error: |
| 481 | output("Unhandled error: {error}", error=str(error)) |
| 482 | sys.exit(127) |
| 483 | finally: |
| 484 | flush() |
| 485 | |
| 486 | return wrapped |
| 487 |
nothing calls this directly
no test coverage detected