草庐IT

try-catch-rethrow

全部标签

node.js - node.js 中的 try-catch-finally block 是否同步?

我有一些代码在一个Node程序的子进程中运行,如下所示:try{vardata=fs.readFileSync(urlPath,{"encoding":"utf8"});}catch(err){console.log("Errorreadingurlfile...");throwerr;}finally{console.log("Fileread!");vararray=data.split("\n");console.log("Found"+array.length+"urls");这段代码是从另一个Node程序调用的,它需要等到这个文件中的所有操作都完成。不幸的是,在执行final

javascript - 如何使用 try、catch 在错误处理中打印消息

我有一个简单的例子:varstr='{"test":1,}'try{JSON.parse(str);}catch(e){console.log(e)}结果:[SyntaxError:Unexpectedtoken}]如何打印所有错误信息?预期结果:undefined:1{"test":1,}^SyntaxError:Unexpectedtoken} 最佳答案 这会有所帮助:varx={asd:"asd",};try{JSON.parse(x);}catch(e){console.log("Error",e.stack);consol

node.js - npm 错误!错误 : connect ECONNREFUSED when trying to update the npm

我正在尝试使用以下命令更新npm(Node包管理器):npminstallnpm@latest-g但我在命令提示符中收到以下错误:npmERR!Windows_NT6.1.7601npmERR!argv"C:\\ProgramFiles\\nodejs\\node.exe""C:\\ProgramFiles\\nodejs\\node_modules\\npm\\bin\\npm-cli.js""install""npm@latest""-g"npmERR!nodev6.9.5npmERR!npmv3.10.10npmERR!codeECONNREFUSEDnpmERR!errnoEC

javascript - s3.getObject().createReadStream() : How to catch the error?

我正在尝试编写一个程序来从s3获取一个zip文件,解压缩它,然后将其上传到S3。但是我发现了两个我无法捕捉到的异常。1.StreamContentLengthMismatch:流内容长度不匹配。收到980323883个字节,共5770104761个字节。这种情况不规律地发生。2.NoSuchKey:指定的键不存在。当我输入错误的键时会出现这种情况。当这两个异常发生时,这个程序就会崩溃。我想正确地捕捉和处理这两个异常。我想防止崩溃。constunzipUpload=()=>{returnnewPromise((resolve,reject)=>{letrStream=s3.getObje

Javascript在try block 内设置const变量

在ES6中是否可以在严格模式下使用const在try{}中设置变量?'usestrict';constpath=require('path');try{constconfigPath=path.resolve(process.cwd(),config);}catch(error){//.....}console.log(configPath);由于configPath定义超出范围,因此无法进行lint。这似乎可行的唯一方法是:'usestrict';constpath=require('path');letconfigPath;try{configPath=path.resolve(p

javascript node.js在try catch中获取行号?

我在node.js脚本上使用trycatch:try{}catch(err){console.log(err)}我得到这样的输出:{stack:[Getter/Setter],arguments:['undefined'],type:'called_non_callable',message:[Getter/Setter]}有没有一种简单的方法可以使信息更丰富?包括行号和函数名等? 最佳答案 那些[Getter/Setter]成员表示有关错误对象的更多信息。您可以使用一个小的辅助函数轻松转储这些getter/setter的内容(非常

javascript - 用一级 try ... catch 捕获 JavaScript Promise 中的错误

所以,我希望我的第一级catch是处理错误的那个。有没有办法将我的错误传播到第一次捕获?引用代码,不工作(还):Promise=require('./framework/libraries/bluebird.js');functionpromise(){varpromise=newPromise(function(resolve,reject){throw('Ohno!');});promise.catch(function(error){throw(error);});}try{promise();}//IWANTTHISCATCHTOCATCHTHEERRORTHROWNINTHE

node.js - 为什么我收到错误 "Trying to open unclosed connection."?

我正在尝试通过mongoose将我的Node应用程序连接到mongodb。它似乎正在工作,因为我可以添加文档,但我收到错误{[Error:Tryingtoopenunclosedconnection.]state:2}.我创建了一个非常简单的应用程序,只是为了在连接我的实际应用程序之前确保一切正常。这是我的简单应用:varmongoose=require('mongoose');varSchema=mongoose.Schema;vartimeSchema=newSchema({timestamp:String});varTime=mongoose.model('Time',timeS

javascript - 为什么不鼓励 `.catch(err => console.error(err))`?

我正在使用Promise,并且代码如下所示:functiongetStuff(){returnfetchStuff().then(stuff=>process(stuff)).catch(err=>{console.error(err);});}或者:asyncfunctiongetStuff(){try{conststuff=awaitfetchStuff();returnprocess(stuff);}catch(err){console.error(err);}}我这样做是为了避免遗漏错误,但一位用户告诉我我不应该这样做,并且不赞成这样做。return….catch(err=>c

powershell - 类型 "bind": bind source path does not exist when trying to run container on Docker for Windows 的安装配置无效

我正在尝试在https://docs.docker.com/engine/admin/prometheus/#use-prometheus上运行Prometheus示例在Windows上使用Docker。我正在执行以下命令:dockerservicecreate--replicas1--namemy-prometheus`--mounttype=bind,source="C:/temp/prometheus.yml",destination=/etc/prometheus/prometheus.yml`--publishpublished=9090,target=9090,protoc