Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 执行nodejs并在出错时获取输出_Node.js_Go_Error Handling_Exec_Testcafe - Fatal编程技术网

Node.js 执行nodejs并在出错时获取输出

Node.js 执行nodejs并在出错时获取输出,node.js,go,error-handling,exec,testcafe,Node.js,Go,Error Handling,Exec,Testcafe,我试图通过go二进制文件中的exec启动一个命令,以获得另一个脚本的JSON输出。另一个脚本是检查html问题的nodejs任务。 如果我在cli上启动nodeJS任务,一切正常,我会得到JSON输出,但如果我在go中启动命令,我只会得到: 退出状态1 我不能完全确定问题是nodejs还是go问题,但即使nodejs发现HTML问题,我也希望能够在go脚本中分析JSON响应 以下是我的源代码: out, err := exec.Command("/usr/bin/testcafe", "'chr

我试图通过go二进制文件中的exec启动一个命令,以获得另一个脚本的JSON输出。另一个脚本是检查html问题的nodejs任务。 如果我在cli上启动nodeJS任务,一切正常,我会得到JSON输出,但如果我在go中启动命令,我只会得到:

退出状态1

我不能完全确定问题是nodejs还是go问题,但即使nodejs发现HTML问题,我也希望能够在go脚本中分析JSON响应

以下是我的源代码:

out, err := exec.Command("/usr/bin/testcafe", "'chromium:headless --no-sandbox'", "--reporter json", "/data/checks.js").Output()
status := http.StatusOK
message := ""
if err != nil {
    status = http.StatusNotImplemented
    message = err.Error() + string(string(out[:]))
    fmt.Println(message)
}

如上所述,如果您需要从Golang中的命令出口访问Stderr,请使用:

message += string(err.(*exec.ExitError).Stderr[:])

在我的例子中,nodejs工具根据问题的数量给出了退出代码。解决了这个问题,它现在运行得很好=)。

我制作了一个函数,用于执行shell命令:


参数是切片中的独立元素。因此,您应该有
“--reporter”,“json”
,并且可能与
chromium…
相同,除非您确实需要带引号的字符串。并且如中所述,您应该检查
exitror.Stderr
。我猜这会告诉你一些关于未知或无效参数的信息。你的实际命令在终端上打印什么?为什么你在
string(string(out[:])中应用
string
两次?
?你如何在终端上写命令?
stdOut, stdErr, err := ShellExec("echo", "hello", "world")
if err != nil {
    // error executing command
}

if stdErr != "" {
    // shell command returned error message
}

if stdOut != "" {
    // shell command returned output message
    // hello world
}