You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.6 KiB

8 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. class KeycloakError(Exception):
  18. def __init__(self, error_message="", response_code=None,
  19. response_body=None):
  20. Exception.__init__(self, error_message)
  21. self.response_code = response_code
  22. self.response_body = response_body
  23. self.error_message = error_message
  24. def __str__(self):
  25. if self.response_code is not None:
  26. return "{0}: {1}".format(self.response_code, self.error_message)
  27. else:
  28. return "{0}".format(self.error_message)
  29. class KeycloakAuthenticationError(KeycloakError):
  30. pass
  31. class KeycloakConnectionError(KeycloakError):
  32. pass
  33. class KeycloakOperationError(KeycloakError):
  34. pass
  35. class KeycloakListError(KeycloakOperationError):
  36. pass
  37. class KeycloakGetError(KeycloakOperationError):
  38. pass
  39. class KeycloakCreateError(KeycloakOperationError):
  40. pass
  41. class KeycloakUpdateError(KeycloakOperationError):
  42. pass
  43. class KeycloakDeleteError(KeycloakOperationError):
  44. pass
  45. class KeycloakProtectError(KeycloakOperationError):
  46. pass
  47. class KeycloakTransferProjectError(KeycloakOperationError):
  48. pass
  49. class KeycloakBuildCancelError(KeycloakOperationError):
  50. pass
  51. class KeycloakBuildRetryError(KeycloakOperationError):
  52. pass
  53. class KeycloakBlockError(KeycloakOperationError):
  54. pass
  55. def raise_error_from_response(response, error, expected_code=200):
  56. if expected_code == response.status_code:
  57. return response.json()
  58. try:
  59. message = response.json()['message']
  60. except (KeyError, ValueError):
  61. message = response.content
  62. if isinstance(error, dict):
  63. error = error.get(response.status_code, KeycloakOperationError)
  64. else:
  65. if response.status_code == 401:
  66. error = KeycloakAuthenticationError
  67. raise error(error_message=message,
  68. response_code=response.status_code,
  69. response_body=response.content)