API & Client - return relevant errors when gpx file is invalid

This commit is contained in:
Sam
2023-04-11 15:58:05 +02:00
parent ba890d90b9
commit c4cec056b4
7 changed files with 106 additions and 7 deletions

View File

@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union
import gpxpy.gpx
from ..exceptions import WorkoutGPXException
from ..exceptions import InvalidGPXException, WorkoutGPXException
from .weather import WeatherService
weather_service = WeatherService()
@ -77,9 +77,12 @@ def get_gpx_info(
"""
Parse and return gpx, map and weather data from gpx file
"""
gpx = open_gpx_file(gpx_file)
try:
gpx = open_gpx_file(gpx_file)
except Exception:
raise InvalidGPXException('error', 'gpx file is invalid')
if gpx is None:
raise WorkoutGPXException('not found', 'No gpx file')
raise InvalidGPXException('error', 'no tracks in gpx file')
gpx_data: Dict = {'name': gpx.tracks[0].name, 'segments': []}
max_speed = 0.0
@ -95,6 +98,10 @@ def get_gpx_info(
segment_start: Optional[datetime] = None
segment_points_nb = len(segment.points)
for point_idx, point in enumerate(segment.points):
if point.time is None:
raise InvalidGPXException(
'error', '<time> is missing in gpx file'
)
if point_idx == 0:
segment_start = point.time
# first gpx point => get weather

View File

@ -15,7 +15,7 @@ from fittrackee import appLog, db
from fittrackee.files import get_absolute_file_path
from fittrackee.users.models import User, UserSportPreference
from ..exceptions import WorkoutException
from ..exceptions import InvalidGPXException, WorkoutException
from ..models import Sport, Workout, WorkoutSegment
from .gpx import get_gpx_info
from .maps import generate_map, get_map_hash
@ -329,6 +329,9 @@ def process_one_gpx_file(
except (gpxpy.gpx.GPXXMLSyntaxException, TypeError) as e:
delete_files(absolute_gpx_filepath, absolute_map_filepath)
raise WorkoutException('error', 'error during gpx file parsing', e)
except InvalidGPXException as e:
delete_files(absolute_gpx_filepath, absolute_map_filepath)
raise WorkoutException('error', str(e))
except Exception as e:
delete_files(absolute_gpx_filepath, absolute_map_filepath)
raise WorkoutException('error', 'error during gpx processing', e)