Catching DOMException in javascript using try catch - javascript

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 ?

Related

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)

Cannot catch UnhandledPromiseRejectionWarning in promise

so I'm trying to catch UnhandledPromiseRejectionWarning in my promise, but for some reason it's not working. It ignores my code and just outputs the error to console.
Error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: Forbidden (Cannot send messages to this
user)
code:
e.message.author.openDM().then((message) => {
message.sendMessage(`test`);
}).catch((error) => {
e.message.channel.sendMessage(error + "test");
});
This is a discord bot, using discordie. In my mind, the above code should send the word "test" to a messages author via private message, if the bot can't, it will send the error and the word test in the channel they sent their message in. However, the second part (inside the catch) doesn't get executed.
tl;dr, The catch in the above code isn't working and I'm getting the above error in console instead if the bot doesn't have permission to dm the user.
You forgot the return statement inside the then function.
I suppose message.sendMessage('test') returns a promise
e.message.author.openDM().then((message) => {
return message.sendMessage(`test`);
}).catch((error) => {
e.message.channel.sendMessage(error + "test");
});

Exception Message in Ajax error callback

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);

JavaScript Websocket catch onerror

Is it possible to catch the browser error when
socket = new WebSocket(WSHost);
fails and try .. catch doesn't work?
Check this jsfiddle and open browser console (F12) and click connect:
http://jsfiddle.net/gsz4kpw4/15/
the "socket.onerror" fires, but there is no error message inside the error event. The browser is logging the message to it's console. Any one know a possibility to catch this message and output with alert?
I also tried this:
function yourCustomLog(msg) {
alert(msg);
}
window.console= yourCustomLog;
But it seems that the browser websocket connect error isn't a real console error. There is no alert when the error occurs.

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).

Categories

Resources