Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error serialization #6937

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions redash/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,17 @@ def serialize_job(job):
job_result = job.latest_result()
if job_result:
if job_result.type == Result.Type.SUCCESSFUL:
result_id = job_result.return_value
result = job_result.return_value
if isinstance(result, Exception):
error = str(result)
status = JobStatus.FAILED
elif isinstance(result, dict) and "error" in result:
error = result["error"]
status = JobStatus.FAILED
else:
result_id = result
else:
error = job_result.exc_string

return {
"job": {
"id": job.id,
Expand Down
81 changes: 81 additions & 0 deletions tests/serializers/test_job.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from unittest.mock import MagicMock

from rq.job import JobStatus
from rq.results import Result

from redash.serializers import (
serialize_job,
)
from redash.tasks.queries.execution import QueryExecutionError
from tests import BaseTestCase


class JobSerializationTest(BaseTestCase):
def test_serializes_job_with_exception_in_result(self):
job = MagicMock()
job.id = 0
job.is_started = False
job.get_status = MagicMock(return_value=JobStatus.FINISHED)
result = MagicMock()
result.type = Result.Type.SUCCESSFUL
result.return_value = QueryExecutionError("test")
job.latest_result = MagicMock(return_value=result)
result = serialize_job(job)
self.assertDictEqual(
result,
{
"job": {
"id": 0,
"updated_at": 0,
"status": JobStatus.FAILED,
"error": str(QueryExecutionError("test")),
"result_id": None,
}
},
)

def test_serializes_job_with_dict_that_contains_error_in_result(self):
job = MagicMock()
job.id = 0
job.is_started = False
job.get_status = MagicMock(return_value=JobStatus.FINISHED)
result = MagicMock()
result.type = Result.Type.SUCCESSFUL
result.return_value = {"error": "test error"}
job.latest_result = MagicMock(return_value=result)
result = serialize_job(job)
self.assertDictEqual(
result,
{
"job": {
"id": 0,
"updated_at": 0,
"status": JobStatus.FAILED,
"error": "test error",
"result_id": None,
}
},
)

def test_serializes_job_with_dict_that_finished_successfully(self):
job = MagicMock()
job.id = 0
job.is_started = False
job.get_status = MagicMock(return_value=JobStatus.FINISHED)
result = MagicMock()
result.type = Result.Type.SUCCESSFUL
result.return_value = 1
job.latest_result = MagicMock(return_value=result)
result = serialize_job(job)
self.assertDictEqual(
result,
{
"job": {
"id": 0,
"updated_at": 0,
"status": JobStatus.FINISHED,
"error": None,
"result_id": 1,
}
},
)
Loading