handle-callback-err要处理的预期错误

Sau*_*abh 10 javascript error-handling jslint eslint vue.js

eslint在我的vue webapp中启用了以下代码:

myApi.get('products/12').then((prodResponse) => {
  state.commit('ADD_PRODUCT', {product: prodResponse.data})
},
error => {
  console.log('Inside error, fetching product line items failed')
  router.push({path: '/'})
})
Run Code Online (Sandbox Code Playgroud)

这是我想要做的错误处理,我仍然从线路中得到以下错误:

http://eslint.org/docs/rules/handle-callback-err 要处理的预期误差〜/ VUE/SRC /存储/模块/ myStore.js:97:9误差=> {

我可以添加以下注释将其转换为警告:

/* eslint handle-callback-err: "warn" */
Run Code Online (Sandbox Code Playgroud)

但是我如何完全抑制它或修改代码,以便不会出现此错误.

Dav*_*art 12

只是有同样的事情,警告信息是误导; 它实际上是因为代码中error没有引用,不是因为它没有被"处理".

确保您执行了错误操作,例如a console.log()或不包含它作为参数:

// do something with error:
error => {
  console.log('Inside error, fetching product line items failed', error)
  router.push({path: '/'})
}

// don't define argument:
() => {
  console.log('Inside error, fetching product line items failed')
  router.push({path: '/'})
}
Run Code Online (Sandbox Code Playgroud)


Sau*_*abh 1

以下是迄今为止在评论的帮助下有效的事情:

如果我设置if如下条件,则不会出现错误:

error => {
  if (error) {
    console.log('Inside error, fetching product line items failed')
    router.push({path: '/'})
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我不想更改代码,则输入以下内容将完全抑制错误:

/* eslint handle-callback-err: "warn" */
Run Code Online (Sandbox Code Playgroud)

期待有更好的建议。