21

I have eslint enabled in my vue webapp, I have following code:

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: '/'})
})

This is the error handling I want to do, still I get following error from the liner:

http://eslint.org/docs/rules/handle-callback-err Expected error to be handled ~/vue/src/store/modules/myStore.js:97:9 error => {

I can add following comment to convert this into warning:

/* eslint handle-callback-err: "warn" */

But how do I suppress this completely or modify code, so that this error doesn't come.

Saurabh
  • 71,488
  • 40
  • 181
  • 244

3 Answers3

38

Just had the same thing, and the warning message is misleading; it's actually because error is not referenced in the code, not because it's not "handled".

Make sure you do something with error, such as a console.log() or don't include it as an argument:

// 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: '/'})
}
Dave Stewart
  • 2,324
  • 2
  • 22
  • 24
2

try this:

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

if there is an error, you need to handle it in your code.

NicoS
  • 21
  • 3
1

Following are the things which have worked so far, from the help of comments:

if I put an if condition like following, error does not come:

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

If I don't want to change the code, putting following will suppress the error completely:

/* eslint handle-callback-err: "warn" */

Looking forward to any better suggestions.

Saurabh
  • 71,488
  • 40
  • 181
  • 244
  • did you try this ? Try writing error as (error) error has more than one arguement – Vinod Louis Nov 25 '16 at 08:49
  • @VinodLouis You mean like this : `(error) => router.push({path: '/'})`, which did not worked. – Saurabh Nov 25 '16 at 08:51
  • (error) => { if (error) { console.log('Inside error, fetching product line items failed') router.push({path: '/'}) } } – Vinod Louis Nov 25 '16 at 08:53
  • Yes, that works, however, for this get promise it will come in error block only in case of error, so that `if(error)` block looks redundant to me. – Saurabh Nov 25 '16 at 08:55
  • then try it like this (error) => {router.push({path: '/'})} – Vinod Louis Nov 25 '16 at 08:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129092/discussion-between-saurabh-and-vinod-louis). – Saurabh Nov 26 '16 at 06:26