question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why exceptions in MethodContext.Build are not caught?

See original GitHub issue

We noticed a behavior while working at our NSpec VS adapter, with a spec class similar to the following:

class SomeSpec: nspec
{
  // ...
  void Given_a_method_context()
  {
    // while building some data used in this context, an exception is thrown
    InputData badInput = null;
    FixtureData expected = new FixtureData(badInput);  // <-- Boom
    FixtureData actual = null;

    before = () => { ... };
    act = () => { actual = ... };

    it["Should test actual Vs expected"] = () => actual.Should().Be(expected);
  }
}

we noticed that, for interested assembly/binary, the whole discovery phase in our adapter fails (gracefully) with 0 found results for the whole binary, even if only that context in that class exposed a problem.

The trigger seems to be an exception thrown when running the main body of method context (note: context body, not the body of before, act, … hooks). In such a scenario:

  1. NSpec.Domain.MethodContext.Build() catches exception only to log a message to console which, incidentally, is not visible when running from within VS test adapter. Then it just throws exception again
  2. As there’s no other catch, the whole call chain up to NSpec.Domain.ContextCollection.Build() fails as well
  3. At this point, our adapter had invoked such code remotely so to run on test assembly. Uncaught exception causes a TargetInvocationException in adapter code, which gets caught and logged in VS test runner output window. But this also causes the whole test discovery on that assembly to fail.

We were thinking whether there’s a way to only make fail a specific context (the one that threw) and its descendants. Any idea? Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
BrainCrumbzcommented, Jun 29, 2016
1reaction
BrainCrumbzcommented, Jun 29, 2016

Not sure we need to do it for anything that takes in a () => {}. Because only “bare” methods are the culprit.

Totally right. I was thinking about “bare” code run within a sub-context context[...] = () => { ... } that is not a hook, e.g.:

// ...
context["Given some sub-context"] = () =>
{
  doSomethingThatThrows();  // <-- This makes the whole sub-context building fail

  before = () => { };

  it[...] = () => { };
};

Does that makes sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java exception not caught?
S.D. Since throw new Exception("2"); is thrown from catch block and not try , it won't be caught again. See 14.20. 2.
Read more >
Exception propagation: When should I catch exceptions?
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has...
Read more >
Specifying the Exceptions Thrown by a Method
If the writeList method doesn't catch the checked exceptions that can occur within it, the writeList method must specify that it can throw...
Read more >
Exceptions - Manual
If an exception is thrown and its current function scope has no catch block, the exception will "bubble up" the call stack to...
Read more >
Entity Framework Core in Action, Second Edition
If not, the ChangeCheck value won't be returned, and EF Core will know that a concurrent ... The timestamp approach catches any update...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found