prismicio / slice-machine

A series of tools for developing and deploying website sections with Prismic
https://prismic.io/docs
Apache License 2.0
287 stars 53 forks source link

[manager] `prismicRepository.pushChanges()` calls `console.error()` when it shouldn't #973

Closed angeloashmore closed 1 year ago

angeloashmore commented 1 year ago

Versions

Reproduction

import { createSliceMachineManager } from "@slicemachine/manager";

const model = {
  id: "foo",
  label: "foo",
  repeatable: false,
  status: true,
  json: {
    Main: {},
  },
};

const manager = createSliceMachineManager();

(async () => {
  try {
    await manager.prismicRepository.pushChanges({
      changes: [
        {
          type: "CustomType",
          id: model.id,
          status: "NEW",
        },
      ],
      confirmDeleteDocuments: false,
    });

    await manager.prismicRepository.pushChanges({
      changes: [
        {
          type: "CustomType",
          id: model.id,
          status: "NEW",
        },
      ],
      confirmDeleteDocuments: false,
    });
  } finally {
    // Only included to clean up the previous changes.

    await manager.prismicRepository.pushChanges({
      changes: [
        {
          type: "CustomType",
          id: model.id,
          status: "DELETED",
        },
      ],
      confirmDeleteDocuments: false,
    });
  }
})();

Steps to reproduce

  1. Log in to Prismic: npx prismic-cli@latest login
  2. Run the above script in a project with a valid slicemachine.config.json.

It pushes a custom type model with the NEW status twice. The second call should trigger an API error which gets logged to the console.

What is expected?

The second manager.prismicRepository.pushChanges() call should throw an error, but it should not log to the console.

What is actually happening?

manager.prismicRepository.pushChanges() calls console.error() when it encounters an API error.

See these lines: https://github.com/prismicio/slice-machine/blob/b4d46f012cba0f46383e38a368eb419de667326b/packages/manager/src/managers/prismicRepository/PrismicRepositoryManager.ts#L404-L409

Logging and handling errors should be the job of the consumer, not @prismicio/manager.

In my specific case, I want to catch the error and try a different status. However, the console.error() signals to the user that something went wrong, despite this being an expected state.

comeprismic commented 1 year ago

Thanks @angeloashmore, I'm closing this issue, the team has transfered it into our technical improvements/debt backlog.