Skip to content

[Feature Request / Bug] Don't process.exit(1) in the API; Throw an error instead #516

@Flaque

Description

@Flaque

Disclaimer: I may be totally off base on what's actually happening here, if I am please let me know 😅

Overview

The API shouldn't quit the process, it should throw an error so that clients can catch the issue.

Issue

When the API fails, it sometimes throws a process.exit(1) which can lead testing frameworks (Jest) to misplace blame. Since Jest (and Ava) run tests in parallel, process.exit(1) can stop the whole shebang in the middle of something leading to weird errors like this:

Screenshot of error

For Screenreaders:

PASS src/lib/frame/test/frame.test.js
RUNS src/lib/core/test/core.test.js
error Command failed with exit code 1.

In the example above, a test that hadn't reported in yet may have called the process.exit(1), but since that interrupted the output of src/lib/core/test/core.test.js, it appears as if that test is the cause.

At the moment, it looks like the api.js node-db-migrate is calling the commands directly via load.

Take the create command, as reproduced below:

create: function (migrationName, scope, callback) {
    var executeCreateMigration = load('create-migration');  // This right here being loaded in
    // ... Other stuff
},

That will call the lib/commands/create-migration.js file, which calls process.exit(1) in several places. For example, if it fails to create a migration:

  createMigrationDir(migrationsDir, function (err) {
    var index = require('../../connect');
    var Migration = require('../migration.js');

    if (err) {
      log.error('Failed to create migration directory at ', migrationsDir, err);
      process.exit(1);
    }

    // ...
  }

Solution

Whenever we would call process.exit(1), check if we're running in the API and instead throw an error? Or maybe redefine process.exit to something like this:

const oldProcessExit = process.exit;

// Before API defs
process.exit = (code) => {
  throw new Error(`Something went wrong in db-migrate! Error code: ${code}`); 
}

// After API defs
process.exit = oldProcessExit;

If this is okay, I would be willing to take a stab at it. If not, no worries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions