From 9363b1457dd02f0534e8f00a42f95b5e534fed74 Mon Sep 17 00:00:00 2001 From: A_D <aunderscored@gmail.com> Date: Tue, 7 Jul 2020 16:59:03 +0200 Subject: [PATCH] Simplified if ladder Removed a large if ladder with a simpler construct that does the same thing --- companion.py | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/companion.py b/companion.py index 785f9ba5..39674403 100644 --- a/companion.py +++ b/companion.py @@ -236,20 +236,10 @@ class Auth(object): if not data.get('code'): print('Auth\tNegative response {!r}'.format(payload)) - - # TODO(A_D): there should be a cleaner way to do this rather than raising in every if - # Additionally, this is basically the same code as seen below, helper method perhaps? - if data.get('error_description'): - raise CredentialsError('Error: {!r}'.format(data['error_description'][0])) - - elif data.get('error'): - raise CredentialsError('Error: {!r}'.format(data['error'][0])) - - elif data.get('message'): - raise CredentialsError('Error: {!r}'.format(data['message'][0])) - - else: - raise CredentialsError() + error = next( + (data[k] for k in ('error_description', 'error', 'message') if k in data), ('<unknown error>',) + ) + raise CredentialsError('Error: {!r}'.format(error)[0]) try: r = None @@ -285,17 +275,8 @@ class Auth(object): print('Auth\tCan\'t get token for {}'.format(self.cmdr)) self.dump(r) - if data.get('error_description'): - raise CredentialsError('Error: {!r}'.format(data['error_description'])) - - elif data.get('error'): - raise CredentialsError('Error: {!r}'.format(data['error'])) - - elif data.get('message'): - raise CredentialsError('Error: {!r}'.format(data['message'])) - - else: - raise CredentialsError() + error = next((data[k] for k in ('error_description', 'error', 'message') if k in data), ('<unknown error>',)) + raise CredentialsError('Error: {!r}'.format(error)[0]) @staticmethod def invalidate(cmdr):