{"record":{"id":"14d534d2f4a5c7c3","repo":"chenhg5/cc-connect","slug":"parse-content-uri-w","errorCode":null,"errorMessage":"parse content URI: %w","messagePattern":"parse content URI: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/matrix/matrix.go","lineNumber":681,"sourceCode":"\t\tif strings.Contains(content.FormattedBody, mention) {\n\t\t\treturn true\n\t\t}\n\t}\n\t// Check plain body for @user:server mention\n\tif strings.Contains(content.Body, selfID.String()) {\n\t\treturn true\n\t}\n\treturn false\n}\n\nfunc (p *Platform) downloadMediaContent(ctx context.Context, contentURL id.ContentURIString) ([]byte, error) {\n\tclient := p.getClient()\n\tif client == nil {\n\t\treturn nil, fmt.Errorf(\"not connected\")\n\t}\n\tparsed, err := contentURL.Parse()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"parse content URI: %w\", err)\n\t}\n\tresp, err := client.Download(ctx, parsed)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer func() { _ = resp.Body.Close() }()\n\treturn io.ReadAll(resp.Body)\n}\n\nfunc (p *Platform) downloadMedia(ctx context.Context, content *event.MessageEventContent) (*core.ImageAttachment, error) {\n\tdata, err := p.downloadMediaContent(ctx, content.URL)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tmime := \"\"\n\tif content.Info != nil {\n\t\tmime = content.Info.MimeType\n\t}","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/matrix/matrix.go#L663-L699","documentation":"downloadMediaContent wraps a parse failure of the Matrix content URI (mxc://...) with \"parse content URI: %w\". Matrix media must be referenced by a valid mxc URI that can be decomposed into server name and media ID before the client can call Download. An unparseable URI means the message event carried malformed or missing media metadata.","triggerScenarios":"An image/file/audio message event contains a msgtype with a content URL that is empty, not an mxc:// URI, or otherwise malformed, so contentURL.Parse() inside downloadMediaContent returns an error.","commonSituations":"Receiving media from a bridge or non-standard client that sets a wrong URL field; an encrypted event where the actual URL is in a nested encrypted payload and the outer URL is blank; homeserver/bridge bugs emitting invalid mxc URIs.","solutions":["Log the offending content URL string to see what was actually received","Verify the event is a plain (non-encrypted) media event; for encrypted rooms read the URL from the decrypted content","Validate the mxc URI format (mxc://<server>/<mediaId>) before passing it, and skip non-media events","If a bridge is producing bad URIs, fix or update the bridge"],"exampleFix":"// before\nresp, err := p.downloadMediaContent(ctx, contentURL)\n\n// after\nif !strings.HasPrefix(string(contentURL), \"mxc://\") {\n    return nil, fmt.Errorf(\"matrix: not an mxc content URI: %q\", contentURL)\n}\nresp, err := p.downloadMediaContent(ctx, contentURL)","handlingStrategy":"validation","validationCode":"u := string(contentURL)\nif !strings.HasPrefix(u, \"mxc://\") || len(strings.TrimPrefix(u, \"mxc://\")) == 0 {\n    // skip: not a valid Matrix media URI\n}","typeGuard":"func isMXC(u id.ContentURIString) bool {\n    _, err := u.Parse()\n    return err == nil\n}","tryCatchPattern":"parsed, err := contentURL.Parse()\nif err != nil {\n    slog.Warn(\"matrix: skipping media with invalid URI\", \"url\", string(contentURL), \"err\", err)\n    return\n}","preventionTips":["Validate mxc:// URIs before downloading","Check the decrypted content for encrypted rooms instead of the outer event","Log rejected URIs to spot misbehaving bridges early"],"tags":["matrix","url-parsing","media-download","validation"],"backgroundTag":"invalid-url-format","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}