Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with no-callback-literal #888

Closed
saratitan opened this issue May 15, 2017 · 12 comments

Comments

@saratitan
Copy link

commented May 15, 2017

I fail to see what's invalid with the following code, what makes this callback literal?

  ], (err, result) => {
    callback(...response(err, (result ? result.paymentProvider : '')))
  })

results in

Unexpected literal in error position of callback  standard/no-callback-literal
@rkurbatov

This comment has been minimized.

Copy link

commented Jun 6, 2017

Confirm, it complains about callback with rested arguments.

@toddself

This comment has been minimized.

Copy link

commented Jun 21, 2017

Also, it is complaining about:

    if (err) {
      log.error(err, 'Cannot get from database')
      return cb({
        message: 'Cannot get data',
        code: 500
      })
    }

We use this frequently since we don't actually want the end user to see the backend message since it is 100% useless to them (and could potentially leak table names, etc), so we return an object that our HTTP framework understands.

@LinusU

This comment has been minimized.

Copy link
Member

commented Jun 22, 2017

@toddself We have a similar setup at work, but we still use Error instances instead of a plain object. Something like this:

class UserError extends Error {
  constructor (...args) {
    super(...args)
    this.name = this.constructor.name
    Error.captureStackTrace(this, this.constructor)
  }
}
app.use((err, req, res, next) => {
  if (err instanceof UserError) {
    // send nice detailed error
  } else {
    // send generic 500 and log error
  }
})
@toddself

This comment has been minimized.

Copy link

commented Jun 22, 2017

This was easy to fix by just moving the object creation to a new variable and returning that variable.

Not sure if this is a bad thing though?

@maxbrunsfeld

This comment has been minimized.

Copy link

commented Jan 5, 2018

I think this rule should simply be removed. Not all callbacks need to take errors as their first argument.

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jan 5, 2018

I’m guessing the rule semantics didn’t take spread operators into account. Perhaps there is a newer version of he rule?

@catdaddy23

This comment has been minimized.

Copy link

commented Jan 6, 2018

@feross feross added this to the standard v12 milestone May 10, 2018

@stale

This comment has been minimized.

Copy link

commented Aug 8, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Aug 8, 2018

@LinusU

This comment has been minimized.

Copy link
Member

commented Aug 8, 2018

I agree. It should be removed all together

I really don't think so. E.g. the code posted by @toddself is exactly the code we want to specifically want to disallow, since it breaks assumptions that people have on how Node.js-style callbacks works.

I’m guessing the rule semantics didn’t take spread operators into account.

This problem would be nice to fix though, this should be reported upstream to ESLint, anyone feel up for doing that?

@stale stale bot removed the stale label Aug 8, 2018

@stale

This comment has been minimized.

Copy link

commented Nov 6, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Nov 6, 2018

@stale stale bot closed this Nov 13, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Feb 11, 2019

@standard standard unlocked this conversation Aug 12, 2019

@feross

This comment has been minimized.

Copy link
Member

commented Aug 12, 2019

@LinusU This problem would be nice to fix though, this should be reported upstream to ESLint, anyone feel up for doing that?

The problem is that we're the maintainers of the no-callback-literal rule ;) https://github.com/standard/eslint-plugin-standard

@standard standard locked and limited conversation to collaborators Aug 12, 2019

@feross

This comment has been minimized.

Copy link
Member

commented Aug 12, 2019

Let's move discussion here: #1352

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
8 participants
You can’t perform that action at this time.