When I turned on the server, it stops if the function JSON.parse() failed to use. Is there any other way to get the return value of JSON.parse()? I want to throw the error when the function doesn't work. When I import data that is not in JSON format, I want to ignore the error in the function.
This can be achieved with a try...catch statement.
The try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
For example, the following code snippet will attempt to parse invalid JSON.
const jsonImportedData = 'not valid JSON';
try {
JSON.parse(jsonImportedData);
} catch(error) {
console.log(error);
}
If you look at the console, you'll see a log with the JSON.parse error message; however, your program will not throw an actual error since it was gracefully handled.
Related
When using an api I often find myself with a rather complicated error object.
Depending on the API that I am using the error texts are quite helpful and I would actually sometimes like to display them directly to the user. The problem, of course, is that the error objects can look quite differently so it would be very verbose to go through them and pick individual objects in case they exists (dependant on the status code of the error).
Is this just the nature of the error object or is there a better way to do this?
What I do to handle API calls that end up with error is this:
try {
const response = await axios.post("Your URL");
// Your code to handle the result
} catch (error) {
console.log(error.response.data.error)
// Code to display the error to the user
}
error.response.data.error is the actual error message sent from the server, not the error code
Occasionally, when I run my puppeteer script, I’ll get a “page crashed” error. It’s most likely not a software error, but something to do with the proxy IPs I’m using/webpages I’m scraping. So all I’m really looking to do is just catch the error, close the current page, and open up a new page.
However, my try catch block doesn’t seem to be working. My code is structured like this:
try {
//puppeteer code
}
catch(e) {
console.log(‘caught error ‘ + e);
}
But when the page crashed error happens, I see “unhandled promise rejection error” instead of my console.log message. What’s going on here?
From here:
page.on('error', msg => {
console.error(...);
throw msg ;
});
And this is also advised to globally catch all uncaught errors in your code:
process.on('unhandledRejection', error => {
console.error(...);
throw error;
});
I have a function which use axios get method and on the promised returned I have added error handling to handle a situation when service I am trying to connect to has been disabled.
axios.get('/someurl')
.then(() => {
// this does not matter
})
.catch((err) => {
logger.error(TAG, 'postCreateVm', err);
return reply(Boom.forbidden(err.message));
});
When I use curl I can see the message, status of response is 403:
# curl -X GET localhost:3000/someurl
{
"message": "abort"
}
The problem is that when I try to access 'message' property i get nothing, but I know it's there! (I have tried to use err.response.data as well with no success also)
According to the documentation I should be able to access it: axios handling errors
What is the proper way to access this message?
I've looked at his code, and it appears the correct response is in the error, but in axios, settle.js masks it with a generic response. You can see the server response by logging the error object in your catch block as stringified JSON:
console.log('caught:::', JSON.stringify(response, null, 2))
So in my case, I fixed it by accessing the returned error as:
error.response.data.message
My catch function received the response property instead of error object. So, to access message I had use:
err.data.message
An npm module generates an error of this format.
throw new Error(error.data.errors)
where error.data.errors is
{ email: [ 'is invalid' ] }
When I try to access it in the catch block of my code, it comes up as
[Error: [object Object]]
How can I access the original error / error message from my catch block?
I just did some experimentation and there doesn't seem to be a lot you can do in this case. It looks like JavaScript calls .toString() on the error message when it's thrown. By default, this is "[object Object]" on all objects.
As far as I can tell, the fix would be to open a pull request that changed the throwing of the error to the following:
throw new Error(JSON.stringify(error.data.errors));
With this change, you could parse the JSON upon catching the error.
try {
// Whatever functionality causes the error.
} catch (e) {
var errors = JSON.parse(e.message);
}
How do I listen to an uncaught exception error in meteor?
The errors are of course specific to the cause, so please can you provide a pseudo style code/explanation that states the concept of handling this error.
An example use case:
A user clicks 'logout' but there is an uncaught exception and the user does not get logged out and so the connection to the server remains open.
You can use the callbacks, they usually provide a reason parameter. E.g
Meteor.call("some_random_non_existent_method", function(err, result) {
if(err) return alert(err.reason)
});
Most meteor callbacks have a error param. With the logout its Meteor.logout(function(err) { ... })
You should be able to catch most types of errors like this. If you want to capture all errors you can use try..catch. Everything in the try block will try to run until it throws an error. You can catch this and provide feedback to the user.
try {
//This will throw an error
this.run_something.go_make_error()
}catch(e) {
alert("There was an error running this");
}