Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  59] [ 6]  / answers: 1 / hits: 17777  / 7 Years ago, thu, may 18, 2017, 12:00:00

I am building an SVG component that takes a filename without extension. I want to force this and throw an error if the extension is used though.



Since I have a ES6 compiler I am simply using this:



if (this.name.includes('.svg')) {
throw 'Do not use the svg extension';
}


Is this a proper way to throw an error in Vue.js or is there a better way? Currently I am getting 2 warnings when I implement this and test it out.



I am getting a [Vue warn] with stacktrace and my own error. Preferably I would just like to throw a simply error message in the console to signify what a user did wrong.



Any thoughts on this approach or tips to handle this better in Vue.js or javscript in general?


More From » vue.js

 Answers
34

You should use the standard TypeError:




Creates an instance representing an error that occurs when a variable or parameter is not of a valid type.




throw new TypeError(Do not use the svg extension, filename)


TypeError-MDN


[#57747] Tuesday, May 16, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josefn

Total Points: 251
Total Questions: 93
Total Answers: 84

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
;