{"record":{"id":"b80f2e30786a1246","repo":"Freika/dawarich","slug":"gpx-parse-error-message","errorCode":null,"errorMessage":"GPX parse error: %{message}","messagePattern":"GPX parse error: %(.+?)","errorType":"exception","errorClass":"Nokogiri::XML::SyntaxError","httpStatus":null,"severity":"error","filePath":"app/services/gpx/track_importer.rb","lineNumber":161,"sourceCode":"      attrs_h = attrs.each_with_object({}) { |a, h| h[a.localname] = a.value }\n      if name == 'trkpt' && @stack.nil?\n        @stack = [attrs_h]\n        @text = +''\n      elsif @stack\n        @stack.last[name] = attrs_h\n        @stack.push(attrs_h)\n        @text = +''\n      end\n    end\n\n    def characters(string)\n      return if @capturing_trk_field && @capture_depth.positive?\n\n      @text << string if @stack || @capturing_trk_field\n    end\n\n    def error(message)\n      raise Nokogiri::XML::SyntaxError, I18n.t('services.gpx.track_importer.parse_error', message:)\n    end\n\n    def end_element_namespace(name, _prefix = nil, _uri = nil)\n      if @capturing_trk_field\n        if @capture_depth.positive?\n          @capture_depth -= 1\n          return\n        end\n\n        if name == @capturing_trk_field\n          assign_trk_identity(@text.strip, @capturing_trk_field)\n          @capturing_trk_field = nil\n          @text = +''\n          return\n        end\n      end\n\n      return if %w[trk trkseg].include?(name)","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/Freika/dawarich/blob/97fad417c5a11b0eb11157890635e015723a2e97/app/services/gpx/track_importer.rb#L143-L179","documentation":"Raised as Nokogiri::XML::SyntaxError by the SAX handler inside Gpx::TrackImporter when Nokogiri's parser reports a well-formedness error while streaming the GPX file. The handler's error callback (invoked by Nokogiri::XML::SAX::Parser on syntax problems) converts it into this RuntimeError with the parser message attached. It means the XML itself is malformed: broken nesting, unclosed tags, invalid characters, or premature truncation.","triggerScenarios":"Importing a .gpx whose XML is truncated (interrupted download/export, partial upload), files with raw '&' or '<' inside text (unescaped ampersands in track names like 'R&D Trail'), wrong encoding declared vs actual bytes, or a non-XML file (HTML error page from a proxy, JSON) renamed .gpx.","commonSituations":"Watch/strava exports interrupted mid-write, files edited by hand and tags left unbalanced, cloud storage proxies returning an HTML login page where GPX was expected, BOM or latin-1 bytes in a file declaring UTF-8.","solutions":["Validate the file standalone first: xmllint --noout file.gpx (or Nokogiri::XML(File.read(path)) { |c| c.errors }) to get the exact syntax error and line.","Re-export or re-download the GPX from the source — truncated/unbalanced XML cannot be repaired reliably in place.","If unescaped characters in a track name are the cause, fix the producing app or escape them (&amp;).","Confirm the file is actually GPX/XML (head -c 400 file) before retrying the import."],"exampleFix":"# before\nGpx::TrackImporter.new(import, user_id, path).call # -> SyntaxError: GPX parse error: ...\n\n# after: pre-validate and give an actionable message\nerrors = Nokogiri::XML(File.read(path)).errors\nif errors.any?\n  import.update!(status: :failed, error_message: errors.first.message)\nelse\n  Gpx::TrackImporter.new(import, user_id, path).call\nend","handlingStrategy":"validation","validationCode":"doc = Nokogiri::XML(File.read(path))\ndoc.errors.empty? || raise(\"GPX file is not valid XML: #{doc.errors.first.message}\")","typeGuard":"def wellformed_xml?(path)\n  Nokogiri::XML(File.read(path)).errors.empty?\nrescue StandardError\n  false\nend","tryCatchPattern":"begin\n  Gpx::TrackImporter.new(import, user_id, path).call\nrescue Nokogiri::XML::SyntaxError => e\n  import.update!(status: :failed, error_message: \"File is corrupted or not valid GPX: #{e.message}\")\nend","preventionTips":["Pre-validate GPX with xmllint or a Nokogiri errors check before starting the import job.","Reject suspiciously small files (truncated uploads) at upload time.","Confirm content, not just extension, when users rename files."],"tags":["gpx","xml","nokogiri","import","parsing","ruby"],"backgroundTag":"xml-parsing-error","analyzedSha":"97fad417c5a11b0eb11157890635e015723a2e97","analyzedAt":"2026-08-21T17:04:17.778Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}