简体   繁体   English

如何处理无法处理的错误?

[英]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:我在 NodeJS 中遇到错误处理问题,在测试我制作的应用程序时,我注意到如果我错误地将 undefined 传递给mongoose.connect()它会给你一个错误,不幸的是,这个错误是没有被回调 function 捕获,也没有被 try & catch 块捕获:

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:您看到错误没有被捕获,请参阅 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. handle = 捕获此问题发生的时间,并向客户端发送一个很好的响应,表明服务器端存在问题。

The moongoose.connect function takes the callback as its third argument , after connection string and options object. moongoose.connect function将回调作为其第三个参数,在连接字符串和选项 object 之后。 It returns a promise, which is getting rejected on the error, but you are never handling that.它返回一个 promise,它被错误拒绝,但你永远不会处理它。

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");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM