Skip to main content

I received a 500 error recently on a PATCH call to update a template. The error response is shown below with the template ID obfuscated)

Upon retrying the exact same request shortly after, the PATCH then succeeded.

I’d like to be able to prevent this issue in the future, in addition to being able to catch it and handle correctly.

  1. Can anyone say why this particular error occured, so I can attempt to avoid it in the future?
  2. If it does occur and there’s no harm in simply repeating the call quickly, is this an appropriate way to handle instances of this error?

{
  "errors": r
    {
      "id": "eaf13aaf-5e7a-4c89-b522-35382b0fe1e1",
      "status": 500,
      "code": "error",
      "title": null,
      "detail": "(1062, \"Duplicate entry 'XXXXXX' for key 'app_templatedefinition.template_id'\")",
      "source": {
        "pointer": "/data"
      },
      "links": {},
      "meta": {}
    }
  ]
}

Hello ​@builder123 , 

The 500 error occurred due to a duplicate entry for the template_id in the database, which suggests a uniqueness constraint violation. This can happen due to a race condition where multiple requests attempt to update the same template simultaneously, a temporary database lock if another process was modifying the same record, or system latency causing a brief inconsistency in processing.

To prevent this issue, ensure that update requests are idempotent and only sent when necessary. Implementing a short delay before retrying, using exponential backoff, can help reduce the chances of encountering the same error again. Checking whether an update is already in progress and using conditional requests, if supported, can also improve stability.

Retrying the request is an appropriate way to handle this error if no unintended side effects occur. Since the retry succeeded shortly after, it indicates a transient issue. Logging the error for further monitoring and implementing a structured retry mechanism with controlled delays would help manage similar occurrences effectively.