Skip to content

ERR_INVALID_CALLBACK error code #411

@AugustinMauroy

Description

@AugustinMauroy

Description

Since the ERR_INVALID_CALLBACK error code is deprecated (DEP0159) and has reached End-of-Life status in Node.js v18.0.0, we should provide a codemod to replace it.

  • The codemod should replace references to ERR_INVALID_CALLBACK with ERR_INVALID_ARG_TYPE.
  • The codemod should handle error code checks in catch blocks and error handling.
  • The codemod should update error message expectations in tests.
  • The codemod should handle both string comparisons and property access patterns.

Function that throw ERR_INVALID_CALLBACK :

  • fs.readFile()
  • fs.readFileSync()
  • fs.writeFile()
  • fs.writeFileSync()
  • fs.appendFile()
  • fs.appendFileSync()
  • fs.unlink()
  • ...

Additional Information

Note that the ERR_INVALID_CALLBACK error code was removed in Node.js v18.0.0. This error code was deprecated because it added more confusion to the errors used for value type validation. Instead, Node.js now uses the more general ERR_INVALID_ARG_TYPE error code for callback validation errors.

When a callback is not a function, Node.js will now throw an ERR_INVALID_ARG_TYPE error instead of ERR_INVALID_CALLBACK.

Examples

Example 1: Error code comparison in catch block

Before:

try {
  fs.readFile("file.txt", "invalid-callback");
} catch (err) {
  if (err.code === "ERR_INVALID_CALLBACK") {
    console.error("Invalid callback provided");
  }
}

After:

try {
  fs.readFile("file.txt", "invalid-callback");
} catch (err) {
  if (err.code === "ERR_INVALID_ARG_TYPE") {
    console.error("Invalid callback provided");
  }
}

Example 2: Error code check in conditional

Before:

function handleError(error) {
  if (error.code === "ERR_INVALID_CALLBACK") {
    return "Please provide a valid callback function";
  }
  return error.message;
}

After:

function handleError(error) {
  if (error.code === "ERR_INVALID_ARG_TYPE") {
    return "Please provide a valid callback function";
  }
  return error.message;
}

Example 3: Test assertion with error code

Before:

const assert = require("node:assert");

assert.throws(
  () => fs.readFile("file.txt", 123),
  { code: "ERR_INVALID_CALLBACK" }
);

After:

const assert = require("node:assert");

assert.throws(
  () => fs.readFile("file.txt", 123),
  { code: "ERR_INVALID_ARG_TYPE" }
);

Example 4: Switch statement with error codes

Before:

switch (error.code) {
  case "ERR_INVALID_CALLBACK":
    console.log("Invalid callback");
    break;
  case "ENOENT":
    console.log("File not found");
    break;
}

After:

switch (error.code) {
  case "ERR_INVALID_ARG_TYPE":
    console.log("Invalid callback");
    break;
  case "ENOENT":
    console.log("File not found");
    break;
}

Example 5: Error matching in test

Before:

assert.throws(
  () => fs.readFileSync("file.txt", "invalid-callback"),
  { code: "ERR_INVALID_CALLBACK" }
);

After:

assert.throws(
  () => fs.readFileSync("file.txt", "invalid-callback"),
  { code: "ERR_INVALID_ARG_TYPE" }
);

Example 6: String includes check

Before:

if (err.toString().includes("ERR_INVALID_CALLBACK")) {
  // Handle callback error
}

After:

if (err.toString().includes("ERR_INVALID_ARG_TYPE")) {
  // Handle callback error
}

Example 7: Multiple error code checks

Before:

try {
  fs.readFile("file.txt", "invalid-callback");
} catch (err) {
  const isCallbackError = 
    err.code === "ERR_INVALID_CALLBACK" || 
    err.code === "ERR_INVALID_ARG_TYPE";
  if (isCallbackError) {
    // Handle invalid callback error
  }
}

After:

try {
  fs.readFile("file.txt", "invalid-callback");
} catch (err) {
  const isCallbackError = 
    err.code === "ERR_INVALID_ARG_TYPE";
  if (isCallbackError) {
    // Handle invalid callback error
  }
}

Refs

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    🔖 Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions