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

Useless Error message [object Object] thrown #1933

Closed
TheColorRed opened this issue Sep 28, 2021 · 2 comments
Closed

Useless Error message [object Object] thrown #1933

TheColorRed opened this issue Sep 28, 2021 · 2 comments

Comments

@TheColorRed
Copy link

I am getting an error that shows little to no information.

[object Object] thrown

After a few days of testing and trying to figure out the issue I decided to modify ExceptionFormatter.message(). Which produced the output needed to figure out what was wrong.

Could we maybe JSON.stringify() the error (maybe with a better approach than below).

      if (error.jasmineMessage) {
        message += error.jasmineMessage;
      } else if (error.name && error.message) {
        message += error.name + ': ' + error.message;
      } else if (error.message) {
        message += error.message;
      } else {
        message += JSON.stringify(error) + ' thrown';
      }
@sgravrock
Copy link
Member

You didn't describe how to reproduce the problem but I'm guessing that you have code that throws something that's not an Error instance. Although JavaScript allows pretty much any value to be thrown, it's a lot harder for tools like Jasmine to provide you with useful information when it's not an Error. For instance, Jasmine can only display stack traces for Error instances because nothing else has them. My guess is that a stack trace would've saved you a lot of debugging time.

The problem with JSON.stringify is that not all objects are JSON serializable, and not all of the ones that are produce a serialization that's as useful in a context like this as the result of toString. I'm open to other suggestions but I think most possible changes would be lateral moves at best, making the output better in some cases and worse in others.

I recommend always using Error instances when throwing or rejecting promises. If you have to use something else, it's a good idea to override toString to return whatever description of the object you'd want to see.

Also, most JS debuggers have the ability to break when an exception is thrown. You might find that useful the next time you run into this kind of problem, whether it's in Jasmine or elsewhere.

@sgravrock
Copy link
Member

Closing due to inactivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants