I use JSON5 parse method to convert the value inserted to the input element to an object
for example inserting WWEew2" to the input element and invoking the method causes an Syntax error
I want to catch the error
SyntaxError: JSON5: invalid end of input at 1:1
at Cu (index.min.js:1)
at ru (index.min.js:1)
at Object.start (index.min.js:1)
at Object.parse (index.min.js:1)
at Vue.displayData (index.html:45)
at invoker (vue.js:2029)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.js:1828)
especially this part
SyntaxError: JSON5: invalid end of input at 1:1
using
const errorString = JSON5.stringify(e)
returns {lineNumber:1,columnNumber:1}
how can I catch the full error and store it as a string in a variable ?
unfortunately the error is not displayed fully in the code snippet example, only if you run it in the browser
new Vue({
el: "#app",
data() {
return {
myInput: "",
};
},
methods: {
displayData() {
try {
console.log(JSON5.parse(this.myInput))
} catch (e) {
console.log(e)
const errorString = JSON5.stringify(e)
console.log(errorString)
// {lineNumber:1,columnNumber:1}
}
},
},
});
<script src="https://unpkg.com/json5@^2.0.0/dist/index.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button type="button" @click="displayData">display</button>
<input v-model="this.myInput" >
</div>