<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def apply_patches(document, patches):
 """Serially apply all patches to a document."""

  for i, patch in enumerate(patches):
    try:
      result = apply_patch(document, patch)
      if patch.op == "test" and result is False:
        raise JSONPatchError("Test patch {0} failed. Cancelling entire set.".format(i + 1))
    except Exception as ex:
      raise JSONPatchError("An error occurred with patch {0}: {1}".format(i + 1, ex)) from ex