This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9808868
commit 7fb0722
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import traceback | ||
from typing import Any, Dict, Optional | ||
|
||
from def_result.ResultDetail import ResultDetail | ||
|
||
|
||
class ErrorDetail(ResultDetail): | ||
""" Stores the error details of a result. """ | ||
errors: Optional[Dict[str, str]] | ||
exception: Optional[Exception] | ||
stack_trace: traceback.StackSummary | ||
|
||
def __init__(self, title: Optional[str] = "An error occurred", | ||
message: Optional[str] = None, | ||
code: Optional[int] = 500, | ||
errors: Optional[Dict[str, str]] = None, | ||
exception: Optional[Exception] = None, | ||
more_data: Optional[Any] = None): | ||
super().__init__(title if title else "An error occurred", message, code, more_data) | ||
self.errors = errors | ||
self.exception = exception | ||
self.stack_trace = traceback.extract_stack() | ||
|
||
def add_or_update_error(self, key: str, value: str): | ||
""" Add or update an error. """ | ||
if self.errors is None: | ||
self.errors = {} | ||
self.errors[key] = value | ||
|
||
def __str__(self): | ||
error_details = super().__str__() | ||
|
||
if self.errors: | ||
error_details += "Errors: " + str(self.errors) + "\n" | ||
|
||
if self.exception: | ||
error_details += "Exception: " + str(self.exception) + "\n" | ||
error_details += "Stack trace: " + str(self.stack_trace) + "\n" | ||
|
||
return error_details |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
|
||
from def_result.ResultDetails.ErrorDetail import ErrorDetail | ||
from tests.helpers import assert_error_detail | ||
|
||
|
||
class TestErrorDetail(unittest.TestCase): | ||
def test_init_without_args(self): | ||
error_detail = ErrorDetail() | ||
|
||
assert_error_detail(test_class=self, error_detail=error_detail, title="An error occurred", code=500) | ||
|
||
def test_init_with_args(self): | ||
exception = Exception("Fake exception") | ||
error_detail = ErrorDetail(title="title", message="message", code=100, errors={"message": "error"}, | ||
exception=exception, more_data=["more_data"]) | ||
|
||
assert_error_detail(test_class=self, error_detail=error_detail, title="title", message="message", code=100, | ||
errors={"message": "error"}, exception=exception, more_data=["more_data"]) | ||
|
||
def test_add_or_update_error(self): | ||
error_detail = ErrorDetail() | ||
self.assertEqual(None, error_detail.errors) | ||
# Add an error | ||
error_detail.add_or_update_error("key", "error") | ||
self.assertEqual({"key": "error"}, error_detail.errors) | ||
# Update an error | ||
error_detail.add_or_update_error("key", "error2") | ||
self.assertEqual({"key": "error2"}, error_detail.errors) | ||
print(str(error_detail)) | ||
|
||
def test_str(self): | ||
error_detail = ErrorDetail(title="title", message="message", code=100, errors={"message": "error"}, | ||
exception=Exception("Fake exception"), more_data=["more_data"]) | ||
|
||
print(str(error_detail)) | ||
self.assertTrue(str(error_detail).startswith( | ||
"Title: title\nMessage: message\nCode: 100\nErrors: {'message': 'error'}\nException: Fake exception")) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters