contentful / contentful-migration

🚚 Migration tooling for contentful
MIT License
325 stars 150 forks source link

Unhandled Promise Rejection when failing to write Error Logs #1099

Open simOneAudi opened 2 years ago

simOneAudi commented 2 years ago

Expected Behavior

When using runMigration as a library function from another piece of software the expected behaviour would be, that all exceptions raised during the execution of migrations to be thrown to the caller.

Actual Behavior

We are calling runMigration from AWS Lambda functions in order to automate updates of our content models. The issue following issue occurs when an error during the execution of any migration occurs:

  1. Something goes wrong and an exception is thrown.
  2. runMigration attempts to write an error log file.
  3. That call fails with EROFS: read-only file system, open '/var/task/errors-2-1656322066050.log' as the filesystem is mounted read-only for AWS Lambda handlers.
  4. The rejection of the writeErrorsToLog promise is not caught, resulting in unhandledRejection event to be raised.
  5. The Lambda handler fails without any of the application's catch blocks to be executed.

Possible Solution

I'd suggest the following two adjustments:

  1. Handle errors thrown by writeErrorsToLog.
  2. Do not write error logs in case runMigration is not used as a CLI (shouldThrow === true).

If someone agrees with these steps, I might be able to provide a PR myself.

Steps to Reproduce

Just execute run migrations from a read-only FS or mock writeErrorsToLog to fail.

Context

Environment

AWS Lambda Container:

simOneAudi commented 2 years ago

I added a proposal on how to resolve the issue: https://github.com/contentful/contentful-migration/pull/1113