Skip to content

Commit

Permalink
Fixed error serialization (#6937)
Browse files Browse the repository at this point in the history
* serialize errors

* lint fix

* cover successful case
  • Loading branch information
AndrewChubatiuk committed May 2, 2024
1 parent b7f22b1 commit bd17662
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
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,
}
},
)

0 comments on commit bd17662

Please sign in to comment.