enwuser
enwuser

Reputation: 3

How to handle the unhandelable errors?

I'm facing a problem with error handling in NodeJS, While I was testing an application that I made, I noticed that if I passed undefined by mistake to the mongoose.connect() it will give you an error, unfortunately, this error is not being caught by the callback function, not either by try & catch blocks:

const envVariable = undefined;

try {
  mongoose.connect(envVariable, (err) => {
    if (err) return console.log("There was an error");
    console.log("success");
  });
} catch (error) {
  console.log("Hah! I caught you");
}

You see the error is not being caught, see the output:

(node:36807) UnhandledPromiseRejectionWarning: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
    at NativeConnection.Connection.openUri (.../nodeJS/tests/node_modules/mongoose/lib/connection.js:680:11)
    at .../nodeJS/tests/node_modules/mongoose/lib/index.js:345:10
    at .../nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (.../nodeJS/tests/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Mongoose._promiseOrCallback (.../nodeJS/tests/node_modules/mongoose/lib/index.js:1135:10)
    at Mongoose.connect (.../nodeJS/tests/node_modules/mongoose/lib/index.js:344:20)
    at Object.<anonymous> (.../nodeJS/tests/server.js:8:12)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:36807) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:36807) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I've faced these kinds of problems many times in my application, but this time I caught one here to open the discussion, how can I stop these kinds of errors to show in my face? and instead, handle them in a nice way?

handle = to catch when this problem happens, and to send a nice response to the client that there was a problem from the server side.

Upvotes: 0

Views: 90

Answers (1)

Bergi
Bergi

Reputation: 664444

The moongoose.connect function takes the callback as its third argument, after connection string and options object. It returns a promise, which is getting rejected on the error, but you are never handling that.

The proper syntax is either

try {
  await mongoose.connect(envVariable);
  console.log("success");
} catch (error) {
  console.log("Hah! I caught you");
}

or

mongoose.connect(envVariable).then(() => {
  console.log("success");
}, (err) => {
  console.log("Hah! I caught you");
});

Upvotes: 1

Related Questions