Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #189 from GustavoHennig/error_code
Browse files Browse the repository at this point in the history
Include the error code returned from OneView to the exception HPOneViewTaskError
  • Loading branch information
danielreed authored Sep 13, 2016
2 parents 30fd820 + f99e24e commit 2d5b46d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
7 changes: 5 additions & 2 deletions hpOneView/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import division
from __future__ import absolute_import
from future import standard_library

standard_library.install_aliases()

__title__ = 'exceptions'
Expand All @@ -20,6 +21,7 @@
__license__ = 'MIT'
__status__ = 'Development'


###
# (C) Copyright (2012-2015) Hewlett Packard Enterprise Development LP
#
Expand All @@ -44,7 +46,6 @@


class HPOneViewException(Exception):

def __init__(self, msg):
self.msg = msg
Exception.__init__(self, msg)
Expand All @@ -55,7 +56,9 @@ class HPOneViewInvalidResource(HPOneViewException):


class HPOneViewTaskError(HPOneViewException):
pass
def __init__(self, msg, error_code=None):
super(HPOneViewTaskError, self).__init__(msg)
self.error_code = error_code


class HPOneViewUnknownType(HPOneViewException):
Expand Down
9 changes: 6 additions & 3 deletions hpOneView/resources/task_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,20 @@ def __wait_task_completion(self, task, timeout):
def __get_task_response(self, task):
if task['taskState'] in TASK_ERROR_STATES and task['taskState'] != 'Warning':
msg = None
error_code = None
if 'taskErrors' in task and len(task['taskErrors']) > 0:
err = task['taskErrors'][0]
if 'message' in err:
msg = err['message']

error_code = err.get('errorCode')

if msg:
raise HPOneViewTaskError(msg)
raise HPOneViewTaskError(msg, error_code)
elif 'taskStatus' in task and task['taskStatus']:
raise HPOneViewTaskError(task['taskStatus'])
raise HPOneViewTaskError(task['taskStatus'], error_code)
else:
raise HPOneViewTaskError(MSG_UNKNOWN_EXCEPTION)
raise HPOneViewTaskError(MSG_UNKNOWN_EXCEPTION, error_code)

deleted_resource = (task['name'] == 'Delete' or task['name'] == 'Remove')

Expand Down
22 changes: 21 additions & 1 deletion tests/unit/resources/test_task_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,28 @@ def test_wait_for_task_with_error_message(self, mock_get, mock_is_running):
self.task_monitor.wait_for_task(task.copy())
except HPOneViewTaskError as e:
self.assertEqual("Error Message", e.msg)
self.assertEqual(None, e.error_code)
else:
self.fail()
self.fail("Expected exception not raised")

@mock.patch.object(TaskMonitor, 'is_task_running')
@mock.patch.object(TaskMonitor, 'get')
def test_wait_for_task_with_error_message_and_error_code(self, mock_get, mock_is_running):

task = {"uri": "uri",
"taskState": "Error",
"taskErrors": [{"message": "Error Message", "errorCode": "ProfileAlreadyExistsInServer"}]}

mock_is_running.return_value = False
mock_get.return_value = task

try:
self.task_monitor.wait_for_task(task.copy())
except HPOneViewTaskError as e:
self.assertEqual("Error Message", e.msg)
self.assertEqual("ProfileAlreadyExistsInServer", e.error_code)
else:
self.fail("Expected exception not raised")

def test_wait_for_task_empty(self):
try:
Expand Down

0 comments on commit 2d5b46d

Please sign in to comment.