Skip to content

Commit e6784d3

Browse files
authored
fix: TT-206 return 404 when a time entry is not running (#274)
* fix: TT-206 return 404 when a time entry is not running * fix typo * fix: TT-206 solve PR comments
1 parent 0d8520e commit e6784d3

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def test_get_running_should_call_find_running(
594594
repository_update_mock.assert_called_once_with(tenant_id, owner_id)
595595

596596

597-
def test_get_running_should_return_not_found_if_StopIteration(
597+
def test_get_running_should_return_no_content_if_StopIteration(
598598
client: FlaskClient,
599599
mocker: MockFixture,
600600
valid_header: dict,
@@ -610,7 +610,7 @@ def test_get_running_should_return_not_found_if_StopIteration(
610610
"/time-entries/running", headers=valid_header, follow_redirects=True
611611
)
612612

613-
assert HTTPStatus.NOT_FOUND == response.status_code
613+
assert HTTPStatus.NO_CONTENT == response.status_code
614614
repository_update_mock.assert_called_once_with(tenant_id, owner_id)
615615

616616

time_tracker_api/api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,9 @@ def default_error_handler(error):
172172
{'message': 'An unhandled exception occurred.'},
173173
HTTPStatus.INTERNAL_SERVER_ERROR,
174174
)
175+
176+
177+
@api.errorhandler(StopIteration)
178+
def handle_no_content(error):
179+
app.logger.error(error)
180+
return {'message': 'No Content'}, HTTPStatus.NO_CONTENT

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def post(self, id):
334334

335335
@ns.route('/running')
336336
@ns.response(HTTPStatus.OK, 'The time entry that is active: currently running')
337-
@ns.response(HTTPStatus.NOT_FOUND, 'There is no time entry running right now')
337+
@ns.response(HTTPStatus.NO_CONTENT, 'There is no time entry running right now')
338338
class ActiveTimeEntry(Resource):
339339
@ns.doc('running_time_entry')
340340
@ns.marshal_with(time_entry)

time_tracker_api/time_entries/time_entries_repository.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,10 @@ def find_running(
324324
)
325325

326326
function_mapper = self.get_mapper_or_dict(mapper)
327-
return function_mapper(next(result))
327+
try:
328+
return function_mapper(next(result))
329+
except StopIteration as no_result:
330+
raise CustomError(HTTPStatus.NO_CONTENT)
328331

329332
def validate_data(self, data, event_context: EventContext):
330333
start_date = data.get('start_date')

0 commit comments

Comments
 (0)