• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Clinical-Genomics / trailblazer / 10418406160

16 Aug 2024 10:18AM UTC coverage: 89.736%. First build
10418406160

Pull #474

github

seallard
Fix imports
Pull Request #474: Add endpoint error handling

60 of 79 new or added lines in 2 files covered. (75.95%)

2177 of 2426 relevant lines covered (89.74%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/trailblazer/server/utils.py
1
import datetime
1✔
2
from http import HTTPStatus
1✔
3
from flask import Request, jsonify
1✔
4
import logging
1✔
5
from functools import wraps
1✔
6
from pydantic import ValidationError
1✔
7

8
from trailblazer.exc import MissingAnalysis
1✔
9
from trailblazer.dto import AnalysesRequest
1✔
10
from trailblazer.services.authentication_service.exceptions import AuthenticationError
1✔
11

12
LOG = logging.getLogger(__name__)
1✔
13

14
def parse_analyses_request(request: Request) -> AnalysesRequest:
1✔
15
    """Parse a request for retrieving analyses."""
16
    query_params = {}
1✔
17
    for key in request.args.keys():
1✔
18
        if key_has_list_of_values(key):
1✔
19
            query_params[key[:-2]] = request.args.getlist(key)
1✔
20
        else:
21
            query_params[key] = request.args.get(key)
1✔
22
    return AnalysesRequest.model_validate(query_params)
1✔
23

24

25
def key_has_list_of_values(key: str) -> bool:
1✔
26
    return key.endswith("[]")
1✔
27

28

29
def stringify_timestamps(data: dict) -> dict[str, str]:
1✔
30
    """Convert datetime into string before dumping in order to avoid information loss"""
31
    for key, val in data.items():
×
32
        if isinstance(val, datetime.datetime):
×
33
            data[key] = str(val)
×
34
    return data
×
35

36

37
def handle_endpoint_errors(func):
1✔
38

39
    @wraps(func)
1✔
40
    def wrapper(*args, **kwargs):
1✔
41
        try:
1✔
42
            return func(*args, **kwargs)
1✔
43
        except AuthenticationError:
1✔
NEW
44
            return jsonify("User not allowed"), HTTPStatus.FORBIDDEN
×
45
        except MissingAnalysis as error:
1✔
46
            return jsonify(error=str(error)), HTTPStatus.NOT_FOUND
1✔
47
        except ValidationError as error:
1✔
48
            LOG.error(f"Validation error in analysis endpoint: {error}")
1✔
49
            return jsonify(error=str(error)), HTTPStatus.BAD_REQUEST
1✔
NEW
50
        except Exception as error:
×
NEW
51
            LOG.error(f"Unexpected error in analysis endpoint: {error}")
×
NEW
52
            return (
×
53
                jsonify(error="An error occurred while processing your request."),
54
                HTTPStatus.INTERNAL_SERVER_ERROR,
55
            )
56

57
    return wrapper
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc