Skip to content

Commit

Permalink
feat: pr recommendation init exc improvemente
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jun 28, 2024
1 parent d718aca commit a9e1fc6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
35 changes: 23 additions & 12 deletions eox_nelp/pearson_vue/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ class PearsonBaseError(Exception):
"""
exception_type = "base-error"

def __init__(self, *args, pipe_frame=inspect.currentframe(), exception_reason="", exception_dict=None):
"""Init pearson exception using frame and exception_reason(str)
Or init using exception_dict representation.
def __init__(
self,
exception_reason,
*args,
pipe_frame=None,
pipe_args_dict=None,
pipe_function=None,
**kwargs
): # pylint: disable=unused-argument
"""Init pearson exception.Is mandatory the exception_reasons.
You could init using pipe_frame
Or init using exception_dict representation with **kwargs.
That representation should have the following shape:
exception_dict = {
'exception_type': 'validation-error',
Expand All @@ -24,16 +33,18 @@ def __init__(self, *args, pipe_frame=inspect.currentframe(), exception_reason=""
}
"""

if exception_dict:
_ = [setattr(self, key, value) for key, value in exception_dict.items()]
super().__init__(exception_dict, *args)
else:
self.exception_type = self.exception_type
self.exception_type = self.exception_type
self.exception_reason = exception_reason

if pipe_frame:
arg_info = inspect.getargvalues(pipe_frame)
self.pipe_args_dict = {arg: arg_info.locals[arg] for arg in arg_info.args}
self.pipe_function = pipe_frame.f_code.co_name
self.exception_reason = exception_reason
super().__init__(self.to_dict(), *args)
else:
self.pipe_args_dict = pipe_args_dict
self.pipe_function = pipe_function

super().__init__(self.to_dict(), *args)

def to_dict(self):
"""
Expand All @@ -55,10 +66,10 @@ def from_dict(cls, exception_dict: dict):
exception_type = exception_dict.get('exception_type')
for subclass in cls.__subclasses__():
if subclass.exception_type == exception_type:
return subclass(exception_dict=exception_dict)
return subclass(**exception_dict)

# Default to Person if no matching subclass is found
return cls()
return cls(**exception_dict)


class PearsonKeyError(PearsonBaseError):
Expand Down
20 changes: 10 additions & 10 deletions eox_nelp/pearson_vue/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ def get_exam_data(user_id, course_id, **kwargs): # pylint: disable=unused-argum
courses_data = getattr(settings, "PEARSON_RTI_COURSES_DATA")
exam_metadata = courses_data[course_id]
except KeyError as exc:
raise PearsonKeyError(pipe_frame=inspect.currentframe(), exception_reason=str(exc)) from exc
raise PearsonKeyError(exception_reason=str(exc), pipe_frame=inspect.currentframe()) from exc
except AttributeError as a_exc:
raise PearsonAttributeError(pipe_frame=inspect.currentframe(), exception_reason=str(a_exc)) from a_exc
raise PearsonAttributeError(exception_reason=str(a_exc), pipe_frame=inspect.currentframe()) from a_exc

# This generates the clientAuthorizationID based on the user_id and course_id
exam_metadata["client_authorization_id"] = generate_client_authorization_id(
Expand Down Expand Up @@ -387,9 +387,9 @@ def build_cdd_request(profile_metadata, **kwargs): # pylint: disable=unused-arg
}
}
except KeyError as exc:
raise PearsonKeyError(pipe_frame=inspect.currentframe(), exception_reason=str(exc)) from exc
raise PearsonKeyError(exception_reason=str(exc), pipe_frame=inspect.currentframe()) from exc
except AttributeError as a_exc:
raise PearsonAttributeError(pipe_frame=inspect.currentframe(), exception_reason=str(a_exc)) from a_exc
raise PearsonAttributeError(exception_reason=str(a_exc), pipe_frame=inspect.currentframe()) from a_exc

return {
"cdd_request": cdd_request
Expand Down Expand Up @@ -426,9 +426,9 @@ def build_ead_request(
"lastUpdate": timezone.now().strftime("%Y/%m/%d %H:%M:%S GMT"),
}
except KeyError as exc:
raise PearsonKeyError(pipe_frame=inspect.currentframe(), exception_reason=str(exc)) from exc
raise PearsonKeyError(exception_reason=str(exc), pipe_frame=inspect.currentframe()) from exc
except AttributeError as a_exc:
raise PearsonAttributeError(pipe_frame=inspect.currentframe(), exception_reason=str(a_exc)) from a_exc
raise PearsonAttributeError(exception_reason=str(a_exc), pipe_frame=inspect.currentframe()) from a_exc

return {
"ead_request": ead_request
Expand All @@ -451,7 +451,7 @@ def validate_cdd_request(cdd_request, **kwargs): # pylint: disable=unused-argum
try:
CddRequest(**cdd_request)
except ValidationError as exc:
raise PearsonValidationError(pipe_frame=inspect.currentframe(), exception_reason=str(exc)) from exc
raise PearsonValidationError(exception_reason=str(exc), pipe_frame=inspect.currentframe()) from exc


def validate_ead_request(ead_request, **kwargs): # pylint: disable=unused-argument
Expand All @@ -469,7 +469,7 @@ def validate_ead_request(ead_request, **kwargs): # pylint: disable=unused-argum
try:
EadRequest(**ead_request)
except ValidationError as exc:
raise PearsonValidationError(pipe_frame=inspect.currentframe(), exception_reason=str(exc)) from exc
raise PearsonValidationError(exception_reason=str(exc), pipe_frame=inspect.currentframe()) from exc


def audit_pearson_error(failed_step_pipeline="", exception_dict=None, **kwargs): # pylint: disable=unused-argument
Expand All @@ -487,14 +487,14 @@ def audit_pearson_error(failed_step_pipeline="", exception_dict=None, **kwargs):
"""
if not exception_dict:
return

if not failed_step_pipeline:
failed_step_pipeline = exception_dict.get("pipe_function")

audit_action = f"Pearson Vue Exception~{exception_dict['exception_type']}"

@audit_method(action=audit_action)
def raise_audit_pearson_exception(exception_dict, *args, **kwargs):
def raise_audit_pearson_exception(exception_dict, failed_step_pipeline):
raise PearsonBaseError.from_dict(exception_dict)

try:
Expand Down
2 changes: 1 addition & 1 deletion eox_nelp/pearson_vue/tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,11 @@ def test_audit_pearson_error(self):
kwargs = {
'exception_dict': {
'exception_type': 'validation-error',
'exception_reason': "error: ['String to short.']",
'pipe_args_dict': {
"cdd_request": {}
},
'pipe_function': 'validate_cdd_request',
'exception_reason': "error: ['String to short.']"
},
"failed_step_pipeline": 'validate_cdd_request',
}
Expand Down

0 comments on commit a9e1fc6

Please sign in to comment.