I am handling the error in a if block in the method where I receive the response from the server. I put the server down in order to test the service unavailable scenario
_onResponse: function (err, res) {
if (err){
}
on doing alert("error " + err);
I receive
Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
at Request.crossDomainError (http://localhost:9000/index.js:33875:14)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:9000/index.js:33945:20)
I want to read the value of Error:
I tried doing err.message but then I receive the entire body of the error.
I tried with err.name I just get the text 'Error'
How do I get the content Request has been terminated in a javascript variable?
From the response all I can say is this is not the error your server is sending. It is raised by the engine while doing XHR to a domain for which CORS is disabled. To catch this error, wrap the XHR code in index.js around line 33945 in a try{} catch(e){} block. Inside catch block you get hold of the error using the e variable.
Your block will handle error only when you get a response from the target server.
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
});
I'm trying to identify an error during submitting Ajax request via jQuery when the connection to the server is failed. I load the page from the server, then disconnect the network connection. The following error handle in the Ajax jquery is used:
error: function(xhr, status, error){
var errorMessage = xhr.status + ': ' + xhr.statusText
alert('Error - ' + errorMessage);
alert('Connection Error\nCould not connect to the server.');
$("#loading").removeClass("show");
$("#submit_btn").removeAttr("disabled");
}
The first alert prints out:
Error - 0: error
I could not able to evaluate the returned errors values to identify the error cause is due to connection failure or something else!
The docs say that an error code of 0 basically means the request could not be completed, while other errors could be due to request completing but some other error in the endpoint or with response.
So usually with the error code 0 you can assume that there was a network issue.
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 am doing a JSON call using the request module in NodeJS but was getting
Error: Parse Error
at Socket.socketOnData (http.js:1367:20)
at TCP.onread (net.js:403:27)
and the process would exit without getting the response.
I put in a
process.on('uncaughtException', function(e){
console.log(JSON.stringify(e, null, ' '))
})
and got this as the trace ..
Parse Error
{
"bytesParsed": 238,
"code": "HPE_INVALID_CONSTANT"
}
I tried putting in a try catch block around the get call but that did not work.
Once I started catching the 'uncaughtException' I got the stack trace and then the JSON response for the call. Please help me identify why this is happening and how I can efficiently handle it. I am using node v0.8.12
HPE_INVALID_CONSTANT means that either the server sent 'HTTP/1.0' at the wrong time or that the Content-Length header was wrong. Either way, it's a problem with the server and its response that will need to be fixed on the server side.
I'm running socket.io on node.js and the socket.io client on an Apache website. If I don't start the node.js server and load the client page, the error event is triggered with an empty error message, which results in the following console output:
GET http://example.com:1337/socket.io/1/?t=1359731838906 socket.io.js:1659
Socket.handshake socket.io.js:1659
Socket.connect socket.io.js:1699
Socket socket.io.js:1551
io.connect socket.io.js:94
(anonymous function) general.js:7
(anonymous function)
What can I do to stop this error being written to the console?
You can not "catch" these errors in the sense that they are not appearing in the console but you can still act upon them by listening for 'connect_error', 'connect_failed' and 'disconnect' of the socket and then either handling them directly or redirecting them to a custom function like handleErrors():
const socket = io.connect(url, { reconnection: false })
socket.on('update', data => console.log(data))
socket.on('connect_error', err => handleErrors(err))
socket.on('connect_failed', err => handleErrors(err))
socket.on('disconnect', err => handleErrors(err))
The only way to hide that error is by never calling io.connect in the first place – which of course would mean your app's socket functions wouldn't work even if the server is up.
It's important to understand that the error message you're seeing is neither something placed there by socket.io itself (via console.error()) nor is it an uncaught JS Exception.
The error message is placed in your console by the browser's XHR object itself. It's telling you that a XHR request has failed (since your server isn't running). The stack trace is telling you what code initiated the XHR request; it isn't a trace of an actual Exception.
Since socket.io must make a request to the server, there's no way it (or you) could prevent that error message from appearing in the console if the server isn't responding.
try
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
Not tested this. Found it here Node.js socket.io-client connect_failed / connect_error event
If you're using Socket.io v4 and need to capture middleware errors, try
// client-side
socket.on("connect_error", (err) => {
console.log(err.message); // prints the message associated with the error
});
source https://socket.io/docs/v4/middlewares/#handling-middleware-error