From backend I am getting specific error message during the post request. How may I get this message in javascript?
catch(err => console.log('error', err.response.data))
With this code I am getting just the type of the error, but I need the message which I throw from backend. F.ex throw ConflictException("The name is already exist"). I need get the following message "The name is already exist", not just the the type.
<!DOCTYPE html>
<html>
<body>
<script>
try {
throw new Error('Custom Exception');
}
catch(err) {
if(err.message){
console.log('inside if & error message as follow : ',err.message);
}else {
console.log('inside else');
console.log(err);
}
}
</script>
</body>
</html>
Above is for demonstration purpose, if it still not working then try
catch(err => console.log('error', err?.message));
Caught errors generally have a message field stored at error?.message or if you using things like Axios or other API requests, error messages are generally at error.response?.data?.message!
Note: ?. is an optional chaining to prevent other errors like Can not read properties of undefined, reading x
Related
I'm making a script which has server side validation. When validation fails I have the script throw an exception server side. I want to access the message in the exception when the response reaches the client side, but I'm having trouble with that. If I console.log the data I can see the exception, but I want to access it in the catch so I can push the error message to an HTML element on the page.
fetch(/* Posting to some PHP script */).then(function(data) {
// Logic here when success
console.log(data) // Shows error msg when there is an error
}).catch(function() {
// Error Handling, I want to push the error msg to HTML element here
});
What am I doing wrong here?
Thanks
Throw the error. It will be caught in the .catch() and you can manipulate it there
fetch(/* Posting to some PHP script */)
.then(function(data) {
// Logic here when success
console.log(data) // Shows error msg when there is an error
if(data.error){ // Or whatever condition you need to detect there's an error
throw data.error; // This will be caught in the catch() below
}
})
.catch(function(err) {
// You have your error here with the message from the server, display it in the HTML
});
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
I will preface this post with I am very new to JavaScript and even newer to AWS and their services.
I am currently working through this AWS tutorial and I am adapting it to fit my needs. I am not asking to make this work, as I already have it working for my needs.
In this bit of code
userPool.signUp('username', 'password', attributeList, null, function(err, result){
if (err) {
alert(err);
return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
});
I am having trouble reformatting the err received by the function call when something goes wrong.
For example, I am using AWS Lambda to check the input validation for server side (using python), and I have it raise Exception("First Name is not long enough") which is what i get in return. However, when the alert(err) is called, correctly, I receive this:
UserLambdaValidationException: PreSignUp failed with error First Name is not long enough..
I have tried split on the err but the console says split is not a function of err.
So my question is, how can I strip this err and only get the message instead of the whole exception?
I have already tried err.split("error"); err.value; err.errorMessage; err["value"]; err["errorMessage"]; err.Error; and it doesn't work.
Also, when I console.log(err) I am presented with:
Error: First Name is not long enough..(...) //the (...) is the stacktrace
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");
}