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.

feat: validateOrReject should throw instance of Error

See original GitHub issue

validateOrReject throws an array of errors that does not have a stack. That is bad in case such error is not being caught and results in 500 Internal Server Error.

Is it possible to add something like validateOrError that would throw an instance like this instead:

class ObjectInvalid extends Error {
  errors: ValidationError[]
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
vlapocommented, Mar 28, 2020

Sure why not. Let’s introduce new validateOrThrow method and mark validateOrReject as deprecated.

3reactions
bogdancommented, Mar 25, 2020

Not all of the rejects are being caught and processed. It means that if the error is displayed in console or sent to exception notification software, you will have no error stack available.

Example jest error when validateOrReject throws:

      161 |     });
      162 |
    > 163 |     it('fails', async () => {
          |     ^
      164 |       await validateOrReject(new Reseller())
      165 |     });
      166 |   });

You may see that jest can only tell the test where the error occurred not the line number and backtrace.

When you see something like this in console:

Failed: Array [
      ValidationError {
       ...
      },
    ]

You have no idea where to fix it.

Any linter will tell you that throwing something that doesn’t inherit Error class is a bad idea:

https://palantir.github.io/tslint/rules/no-string-throw/ https://eslint.org/docs/rules/no-throw-literal

BTW, this is first time I see someone throws an Array. IMO it is even more terrible idea than throwing a string because when you see it in console you don’t even realize what had happened.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test the type of a thrown exception in Jest
Really like this one as my code needs to test a value of the thrown error so I need the instance. I would...
Read more >
How to use the class-validator.validateOrReject function in ...
validateOrReject examples, based on popular ways it is used in public projects. ... await validateOrReject(validation) } catch (error) { throw new this.
Read more >
Better error handling in JavaScript | by Iain Collins - Medium
It's best to avoid throwing errors from inside a Promise, because they may not always be caught, depending on how the code that...
Read more >
npm:aws-lambda-framework | Skypack
Errors are automatically logged and optionally send to a Slack channel of your choice. New Lambda functions must implement an invoke function, which...
Read more >
Basic Error Handling with Exceptions | C# Methods ... - InformIT
For example, assume the user enters “forty-two” for the age in the previous example. In this case, int.Parse() will throw an exception of ......
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