Exception Message in Ajax error callback - javascript

I have triggering an Ajax call and in the error callback I am trying to access the exception message(Not the Response Text). I am throwing an exception like this:
throw new Exception("Please enter a response")
Now I want to get the above message and display it in alert box.
I searched stackoverflow and found this:
error: function(e,status){
var err = eval("(" + e.responseText + ")");
alert(err.Message);
}
but the above doesn't work.
I am getting the response text but not able to access that particular message.
The error that I am getting is Uncaught SyntaxError: Unexpected token <

When your server throws an Internal Server Error, from Javascript side it's still a succes. how about adding the Status code response instead of throwing exception from the backend
return new HttpStatusCodeResult(400, "Custom Error Message 2");

You can refer this and try to get error message as alert(err.message); (lowercase) not alert(err.Message);

Related

Catching DOMException in javascript using try catch

I am trying to catch the following error,
DOMException: NFC permission request denied
So I wrote a try catch and successfully caught an error which printed the above in console
console.log(error); // prints above
if (error === "DOMException: NFC permission request denied"){
console.log("THIS IS WHAT I WANT");
}
But the console log message never shows, is my if statement incorrect ?

How do I send a custom error message from Node, and display it with catch?

I'm trying to send a custom error message from node, receive it on the frontend, and display it.
On server I'm trying to do:
res.status(500).send('Something broke!')
And on client I'm doing:
try {
const res = await axios.post(`${API_URL}/profiles/signup`, credentials)
} catch(err) {
console.log("Sign up error:", err)
}
What I want is to get something like this in my console:
Sign up error: Something broke!
but I'm getting:
Sign up error: Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
How do I access the custom message I'm sending?
You can create an error with the help of Error()
throw new Error({code: 500, message: 'Sign up error: Something broke!'});
throw send your error to the catch block automatically.
Figured it out:
console.log("Sign up error:", err.response.data)

AngularJS: error in console despite handling it

I don't know if I am doing something incorrect here or what, but I have a question regarding error handling in angular.
I've gone through several similar questions on the website but they don't answer my question or solve my problem.
So this is basically what I'm trying to do:
var deferObject = $q.defer();
$http(some_url + user_entered_value).then(function(response){
deferObject.resolve(response.data);
}, function(error){
//error handling code here
deferObject.reject(error.data);
});
The server returns an error object which looks like this: {"status":"500","error":"Internal Server Error"}
Whenever there is an error in the application, angular throws an error by logging it in the console: "failed to load resource: Internal server error".
So here are my questions:
How do I prevent the angular error logging?
How do I handle that error?
I tried doing this:
var deferObject = $q.defer();
$http(some_url + user_entered_value).then(function(response){
deferObject.resolve(response.data);
}, function(error){
//error handling code here
deferObject.reject(error.data);
}).catch(function(e){
throw e;//to handle the error
});
But I still get the error in the console.

Node.js - printing details about unexpected error?

Does anybody know how to print more details about unexpected error that occurred?
I have this piece of code:
process.on('uncaughtException', function(err)
{
console.log("Unexpected error occurred. " + err + ".");
process.exit(1);
});
...and when error occurs this is printed: "Unexpected error occurred. Error: connect ECONNREFUSED."
There is no details about this error. Like what exactly failed, which connection, ip:port. How can I fetch that kind of data? And I don't want to use Domain module (in case somebody suggests).

What error is thrown when trying to appendChild script element fails due to unauthorized

I want to catch a specific failure of this JavaScript code:
var script = $wnd.document.createElement('script');
script.setAttribute('src', url);
script.setAttribute('type', 'text/javascript');
When the url where the script resides needs the user to be logged in, and so returns an HTTP 401 Unauthorized error.
None of the values I understand that error (in a try/catch) can take on seem to match very well.
EvalError: An error in the eval() function has occurred.
RangeError: Out of range number value has occurred.
ReferenceError: An illegal reference has occurred.
SyntaxError: A syntax error within code inside the eval() function has occurred. event.
TypeError: An error in the expected variable type has occurred.
URIError: An error when encoding or decoding the URI has occurred (ie: when calling encodeURI()).
Is there any way to catch specifically this 401 error, or at least the class of IO error that would be thrown by not being able to load the script.
Thanks
script.addEventListener('error', function(){
// Didn't load
}, true);

Categories

Resources