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.

Cannot handle thrown if not derived from Error

See original GitHub issue
import { expect } from "chai";

class Exception {}

describe("chai", () => {
	it("should handle thrown object", () => {
        expect(()=> {
            throw new Exception();
        }).to.throw(Exception);
    });
});
  1. chai should handle thrown object: AssertionError: expected [Function] to throw ‘Exception’ but {} was thrown

currently have to wrap as follows:

import { expect } from "chai";

class SentinelNoError {}
const sentinelNoError = new SentinelNoError();

function returnCatch(fn: ()=>void): any {
    try {
        fn();
        return sentinelNoError;
    } catch(error) {
        return error;
    }
}

class Exception {}

describe("chai", () => {
	it("should handle thrown object", () => {
        expect(returnCatch(()=> {
            throw new Exception();
        })).to.be.instanceof(Exception);
    });
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
meebercommented, Jul 24, 2018

@jseter The ability to provide arguments inside of the .throw assertion is just meant as a shortcut for the most common use cases, allowing a user to perform multiple assertions internally on the thrown value with a single assertion. It’s not meant to cover every possibility. The more possibilities it covers, the more complex (for Chai) and troublesome (for users) the code becomes, as described here.

Although this branch hasn’t been merged into Chai yet, the approach documented here should work for you in the current version of Chai.

import { expect } from "chai";

class Exception {}

describe("chai", () => {
  it("should handle thrown object", () => {
        expect(()=> {
            throw new Exception();
        }).to.throw().an.instanceof(Exception);
    });
});
1reaction
meebercommented, Aug 1, 2018

@liuxh0 The good news is that using the .throw() assertion without any parameters causes it to only care about whether or not the function threw. It doesn’t care if an Error object was thrown, or some other object, or a string, or even undefined. And you can chain any of Chai’s other assertions (e.g., instanceof) afterward to make assertions on the thrown value.

The behavior of .throw when one or more parameters are passed isn’t quite right, I think. It’s likely that the solution I worked on last year isn’t perfect either. It’s a fair point that .throw shouldn’t care what type of value is thrown, even when passing parameters to .throw; otherwise, as you said, it’d be more like .throwError in that case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exceptions and Error Handling, C++ FAQ - Standard C++
As a rule of thumb, exception handling is extremely cheap when you don't throw an exception. It costs nothing on some implementations. All...
Read more >
Correct Exceptions in C++ - Stack Overflow
First, exceptions let you segregate the code that deals with errors, so the main idea and readability of the code don't get lost...
Read more >
Top 15 C++ Exception handling mistakes and how to avoid ...
Mistake # 6: Not Throwing an exception by value ... If you throw a pointer to an exception object, you'll need to deal...
Read more >
Modern C++ best practices for exceptions and error handling
In modern C++, in most scenarios, the preferred way to report and handle both logic errors and runtime errors is to use exceptions....
Read more >
C++ Tutorial: Handling Exceptions - 2020 - BogoToBogo
If a program wants to open a file, it is not a normal flow, because the user ... But we can signal an...
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