contentful / contentful-management.js

JavaScript library for Contentful's Management API (node & browser)
https://contentful.github.io/contentful-management.js
MIT License
264 stars 97 forks source link

Updating contentTypes programmatically gives generic error messages #2257

Open danieljameswilliams opened 3 months ago

danieljameswilliams commented 3 months ago

We are trying to update an existing contentType programmatically using the contentful-management SDK, but when there are conflicts of some sort, we don't get any meaningful error messages. The response would often be a 409 - Conflict - and then the JSON tried to process

What we would expect is some kind of detailed explanation of the error, so we can resolve it. We believe this is a quite important feature, because when migrating contentTypes, if anything fails we have to delete all content to start over, which isn't an option when we go live.

In an attempt to figure out the error I deleted the contentType from the web interface and tried to deploy again, and here it gave a meaningful error: 422 - Unprocessable Entity - Field "X" mapped multiple times in "editorLayout"

export async function updateContentType(environment: Contentful.Environment, config: ContentTypeDefinition) {
  const contentType = await environment.getContentType(config.id)
  if (contentType) {
    contentType.name = config.name
    contentType.displayField = config.displayField
    contentType.description = config.description
    contentType.fields = config.fields

    const updatedContentType = await contentType.update()
    const publishedContentType = await updatedContentType.publish()
    const editorInterface = await publishedContentType.getEditorInterface()
    editorInterface.controls = config.controls
    editorInterface.editorLayout = config.editorLayout
    editorInterface.groupControls = config.groupControls
    await editorInterface.update()
  }
}
{
  "VersionMismatch": {
    "status": 409,
    "statusText": "Conflict",
    "message": "",
    "details": {},
    "request": {
      "url": "/spaces/X/environments/dev/content_types/X",
      "headers": {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/vnd.contentful.management.v1+json",
        "X-Contentful-User-Agent": "sdk contentful-management.js/11.25.0; platform node.js/v21.7.3; os Windows/v21.7.3;",
        "Authorization": "Bearer X",
        "User-Agent": "axios/1.6.8",
        "Content-Length": "1726",
        "Accept-Encoding": "gzip, compress, deflate, br"
      },
      "method": "put",
      "payloadData": "X"
    },
    "requestId": "X"
  }
}

We are using "contentful-management": "^11.25.0"