basecamp/trix · error

unknown content type: ${contentType}

Error message

unknown content type: ${contentType}

What it means

ES-module version of the same guard in src/trix/core/serialization.js: serializeToContentType throws when the contentType key is absent from the serializers map. Trix supports only "application/json" and "text/html" for serialization.

Source

Thrown at src/trix/core/serialization.js:91

  },
}

const deserializers = {
  "application/json": function(string) {
    return Document.fromJSONString(string)
  },

  "text/html": function(string) {
    return HTMLParser.parse(string).getDocument()
  },
}

export const serializeToContentType = function(serializable, contentType) {
  const serializer = serializers[contentType]
  if (serializer) {
    return serializer(serializable)
  } else {
    throw new Error(`unknown content type: ${contentType}`)
  }
}

export const deserializeFromContentType = function(string, contentType) {
  const deserializer = deserializers[contentType]
  if (deserializer) {
    return deserializer(string)
  } else {
    throw new Error(`unknown content type: ${contentType}`)
  }
}

View on GitHub (pinned to 4700401311)

Solutions

  1. Restrict calls to "application/json" or "text/html".
  2. Add a whitelist check or default branch before calling serializeToContentType.
  3. Log/inspect the actual contentType value — undefined produces the bare "unknown content type: undefined" message.
  4. Preprocess unsupported types (e.g. HTML-parse) into a supported format.

Example fix

// before
export const serialize = (data, type) => serializeToContentType(data, type)
// after
const SUPPORTED = ["application/json", "text/html"]
export const serialize = (data, type) => {
  if (!SUPPORTED.includes(type)) type = "text/html"
  return serializeToContentType(data, type)
}
Defensive patterns

Strategy: validation

Validate before calling

const SUPPORTED = ["application/json", "text/html"]
if (!SUPPORTED.includes(contentType)) {
  contentType = "text/html"
}

Type guard

const isSupported = (ct) => typeof ct === "string" && ["application/json", "text/html"].includes(ct)

Try / catch

try {
  return serializeToContentType(data, contentType)
} catch (e) {
  if (/^unknown content type/.test(e.message)) {
    return serializeToContentType(data, "text/html")
  }
  throw e
}

Prevention

When it happens

Trigger: importing serializeToContentType from trix core and calling it with any contentType other than "application/json"/"text/html", including undefined/null or custom types like "text/plain".

Common situations: Building custom attachment serialization with a MIME type; refactors that renamed content-type constants; server-driven content types passed straight through.

Related errors


AI-assisted analysis of basecamp/trix@4700401311 (2026-09-02). Data as JSON: /api/errors/4098086066daafd7. Report an issue: GitHub.