My objective: Test out my error handling functionality.
Temporary solution: Have a custom route: /error
, which contains code which purposefully produces fatal error.
var a = undefined;
a.b.c // Breaks.
The above works, but I can't use it to test production site as the page is not required.
I was looking for a way to test it via the browser. I tried simply adding"
throw new Error("Custom error thrown here")
to the console. That doesn't actually break it during runtime.
I tried adding a break point and adding the same code: throw new Error("Custom error thrown here")
. That didn't work either.
Any other easier ways to do this rather than the above?
I was looking for a way where I can do it via browser only.
Thanks.
CodePudding user response:
I would use the exec function which actually takes string and runs the code within at compile time.
exec('a.b.c')
CodePudding user response:
You won't be able to throw an error inside your application from the console, since you are out of scope of the app.
Having said that, one slightly awkward way you could do this is by adding a breakpoint at the start of the javascript file.
Reload the page and your app will pause at the breakpoint - you can then modify the code as you need - like adding a throw new Error("something...")
- and save your edits.
Then allow the code to run and you will see your error.
An obvious downside is if you reload the changes will be gone, but I believe it's as close as you can get to modifying code at runtime.