Skip to content

Rule proposal: prefer-t-throws #156

Open
@gajus

Description

@gajus
Contributor

Issuehunt badges

I was reviewing some codes using ava and found this pattern repeating often:

try {
  await request(requestOptions);
} catch (error) {
  t.true(error.statusCode === 500);
}

This can be written instead as:

const error = await t.throws(request(requestOptions));

t.true(error.statusCode === 500);

I think the latter should be preferred.


IssueHunt Summary

Backers (Total: $80.00)

Submitted pull Requests


Become a backer now!

Or submit a pull request to get the deposits!

Tips

Activity

jfmengels

jfmengels commented on Nov 13, 2016

@jfmengels
Contributor

Sounds like a good idea to me 👍. Not sure when/why try/catch makes sense in tests when t.throws is available.

The second example is better, but FYI, using t.fail() would improve your first example.

try {
  await request(requestOptions);
  t.fail();
} catch (error) {
  t.true(error.statusCode === 500);
}
gajus

gajus commented on Nov 13, 2016

@gajus
ContributorAuthor

The second example is better, but FYI, using t.fail() would improve your first example.

Another idea for a lint rule. :-)

changed the title [-]rule proposal: no try-catch[/-] [+]Rule proposal: `no-try-catch`[/+] on Nov 14, 2016
sindresorhus

sindresorhus commented on Nov 14, 2016

@sindresorhus
Member

👍 Sounds good. PR welcome :)

I think prefer-t-throws would be a better name. We don't want to prevent all usage of try/catch. Thoughts?

jfmengels

jfmengels commented on Nov 14, 2016

@jfmengels
Contributor

Agreed

changed the title [-]Rule proposal: `no-try-catch`[/-] [+]Rule proposal: `prefer-t-throws`[/+] on Nov 14, 2016
gajus

gajus commented on Nov 14, 2016

@gajus
ContributorAuthor

I think prefer-t-throws would be a better name. We don't want to prevent all usage of try/catch. Thoughts?

What would be a valid use case for try..catch that cannot translate to t.throws?

jfmengels

jfmengels commented on Nov 14, 2016

@jfmengels
Contributor

I think that t.throws() handles every case you'd use a try catch for, and even the Promise#catch() case.

sindresorhus

sindresorhus commented on Nov 14, 2016

@sindresorhus
Member

@gajus Maybe something that might throw, but that you don't care about in the test, so you'd like to silence it. I'm sure there are other cases we can't think of too. But the main point is that the intent is clearer with prefer-t-throws than no-try-catch; We want to recommend using t.throws(), not arbitrarily prevent try/catch.

vadimdemedes

vadimdemedes commented on Nov 14, 2016

@vadimdemedes

The immediate valid use case for try..catch that came up in my mind is error handling in Koa:

app.use(function * (next) {
  try {
    yield* next;
  } catch (err) {
    // handle error
  }  
});
vadimdemedes

vadimdemedes commented on Nov 14, 2016

@vadimdemedes

We don't want to prevent all usage of try/catch.

Definitely agree with this.

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @sindresorhus@vadimdemedes@gajus@jfmengels@IssueHuntBot

      Issue actions

        Rule proposal: `prefer-t-throws` · Issue #156 · avajs/eslint-plugin-ava