AngularJS: error in console despite handling it - javascript

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.

Related

Sending 500 ("Server Error") response when visiting any url in Sails App

I am using Sails and Mysql to handle the database of my app. I recently clicked on a chrome autofill URL that took me to a 404'd page, since that url localhost:1337/companies was not active in this project.
Ever since I did that, my app returns
error: Sending 500 ("Server Error") response:
Error (E_UNKNOWN) :: Encountered an unexpected error
: ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'where clause'
and
Details: Error: ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'where clause'
any time I go to a url in my app. I tried cloning my app and relinking the database to no avail. I see this is a common issue people run into with sails, but none of the mentioned solutions were able to fix it for me.
Everything was working before I clicked that URL which makes me believe it may be a route issue, but the app still works, it will just fill up my terminal with the above errors.
Any help is greatly appreciated!
Edit: this is the custom API call I was working on in my controller.js when I started seeing the error:
winner: function (req, res) {
User.findOne({id: req.param('id')}).exec(function(err, user) {
if (err) {
return res.negotiate(err);
}
else {
var u = user;
u.totalwins++;
u.save();
sails.io.sockets.emit("chat", {verb:"score", data:{id: req.param('id'), name: user.name}});
}
});
}
Also receiving this error in my chrome-browser console:
:1337/user/quizzes Failed to load resource: the server responded with a status of 500 (Internal Server Error)

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

Websocket-Rails Channel 'Error during WebSocket Handshake'

I'm trying to use websocket-rails to broadcast live routes as they come in to my site. However, I'm unable to get any messages to be published. I've read all of the help I could find, and I can't seem to get a solution.
Here is what the javascript I have in application.html.erb:
var route_name = window.location.pathname + window.location.search;
$.post("/realtimeroutes",
{ data: { route_name: route_name} },
function(response) {
}
);
This data is handed off to a rails controller method realtime_routes which looks like this:
def realtime_routes
puts '*** The Route Data is: ' + params[:data][:route_name]);
new_route = RouteCatcher.new(route_name: params[:data][:route_name]
if new_route.save
route = {route_name: params[:data][:route_name]}
puts '*** Right before the Message is sent ***'
WebsocketRails[:new_route].trigger('new', route)
puts '*** Right After the Message is sent ***'
render nothing: true
else
render nothing: true
end
end
To this point everything is fine. updates are happening in the DB and both the before and after messages are printing to the console, but there is no Websocket Message fired.
Next I have this javascript listening in realtime.html.erb:
var dispatcher = new WebSocketRails(window.location.host + '/websocket');
var channel = dispatcher.subscribe('new_route');
channel.bind('new', function(route) {
console.log('********** Inside Bind Event');
console.log('********** a new route '+ route.route_name +' arrived!');
});
Nothing is ever being received on the bind event and when I look in the Console on Chrome, I get:
'Uncaught ReferenceError: WebSocketRails is not defined'
So I'm absolutely stuck here as I've followed every bit of documentation I can find and really have no idea where to go. Any help with this is greatly appreciated.
UPDATE: That was an initialization error because the WebSocketRails was being rendered before the rest of the application js.
However, now I am getting the error:
WebSocket connection to 'ws://localhost:3000/websocket' failed: Error during WebSocket handshake: Unexpected response code: 301
After reading further about other people with the same error, it seems prudent to mention that I am using Puma on AWS elasticbeanstalk with SSL in production.

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