Freika/dawarich · warning

Failed to parse track segments for marker update:

Error message

Failed to parse track segments for marker update:

What it means

The track-segment marker update path reads feature.properties.segments the same way as the click path: a string value is passed to JSON.parse, and a malformed string throws. Unlike the click path, this handler returns early on failure, so segment markers are not created at all for that track.

Source

Thrown at app/javascript/controllers/maps/maplibre/event_handlers.js:1093

   * Update track segment markers when track geometry changes
   * Called after track recalculation to move emoji markers to new positions
   * @param {Object} feature - The updated track GeoJSON feature
   */
  updateTrackMarkers(feature) {
    if (!this.selectedTrackFeature) return
    if (!feature || !feature.geometry || feature.geometry.type !== "LineString")
      return

    // Parse segments from feature properties
    let segments = []
    try {
      const props = feature.properties || {}
      segments =
        typeof props.segments === "string"
          ? JSON.parse(props.segments)
          : props.segments || []
    } catch (err) {
      console.warn("Failed to parse track segments for marker update:", err)
      return
    }

    const trackId = feature.properties?.id
    this._createTrackSegmentMarkers(trackId, feature, segments)
  }

  /**
   * Zoom the map to fit a specific segment's bounds
   * @param {Object} segment - Segment data with start_index and end_index
   */
  _zoomToSegment(segment) {
    if (!this.selectedTrackFeature || !segment) return

    const coords = this.selectedTrackFeature.geometry?.coordinates
    if (!coords || coords.length < 2) return

    const startIdx = Math.max(0, segment.start_index || 0)

View on GitHub (pinned to 97fad417c5)

Solutions

  1. Inspect the failing feature's properties.segments in the debugger to find the exact JSON syntax error
  2. Fix the producing serializer or importer to emit valid segment JSON
  3. Share one safe-parse helper between the click path and the marker path so both degrade identically
  4. If markers matter for this track, fall back to re-fetching via fetchTrackWithSegments instead of returning silently

Example fix

// before
try {
  const props = feature.properties || {}
  segments =
    typeof props.segments === "string"
      ? JSON.parse(props.segments)
      : props.segments || []
} catch (err) {
  console.warn("Failed to parse track segments for marker update:", err)
  return
}
// after
const props = feature.properties || {}
const segments = safeParseSegments(props.segments) // shared helper, never throws
if (!segments.length) return
this._createTrackSegmentMarkers(trackId, feature, segments)
Defensive patterns

Strategy: validation

Validate before calling

const raw = feature.properties?.segments
const looksParsable =
  Array.isArray(raw) ||
  (typeof raw === "string" && raw.trimStart().startsWith("["))
if (!looksParsable) return // avoid JSON.parse on obviously wrong payloads

Type guard

/** @param {unknown} raw @returns {boolean} */
function isParsableSegments(raw) {
  if (Array.isArray(raw)) return true
  if (typeof raw !== "string") return false
  const t = raw.trim()
  return t.startsWith("[") && t.endsWith("]")
}

Try / catch

try {
  segments = safeParseSegments(props.segments)
} catch (err) {
  console.warn("Failed to parse track segments for marker update:", err)
  return // skip markers for this track only; do not break the surrounding loop
}

Prevention

When it happens

Trigger: A track feature whose segments property is an invalid JSON string (truncated response, double encoding, NaN in the payload); features built by hand or cached from an older API version; marker refresh triggered by a live track update that carries a partial payload.

Common situations: Marker updates after live track pushes with incomplete data; switching between tiled and non-tiled point sources; stale caches from an upgraded backend; tracks imported by older importers that produced malformed segment JSON.

Understand the failure class

Related errors


AI-assisted analysis of Freika/dawarich@97fad417c5 (2026-08-21). Data as JSON: /api/errors/083f9c301ee2f7c3. Report an issue: GitHub.