I'm developing a website with server-side JScript engine over ASP server.
I have several try-catch clauses in my code looking roughly like this:
try {
// do something
}
catch (err) {
// pass it to the frontend code
die("Exception caught: " + err.description);
}
I would very much like to display the line number in which the error occurred. The filename would be a nice bonus but it's not very important.
How can it be done?
Thanks!
The err object (of type ASPError) has Line and File properties - just what you need (see this for more properties).
Related
I'm using npm yahoo-finance to fetch stock data. When I input a stock symbol that doesn't exist, I would like to catch the error.
const yahooFinance = require('yahoo-finance');
async function stockData() {
try {
let data = await yahooFinance.historical({symbol: "SIJGAOWSFA", from: 2020-08-23, to: 2021-08-23});
} catch (error) {
console.error(error)
}
}
stockData();
However it doesn't appear to be a typical fetch error. It's not being caught at all. By that I mean, the error you see below was not logged to the console via the console.error(error). Rather something outside the scope of this file is logging the error. When the error occurs, nothing in catch is executed.
I plan on using this in a for loop, so would like to catch the error so I can avoid executing any following functions.
A collaborator says that:
Is this from an existing project that was working and stopped working, or a new project?
If the former - everything is still working fine on my side. (Very) occasionally there are issues at yahoo that get stuck in their cache, possibly relating to DNS too. I'd suggest to clear your DNS cache and also try querying different data to see if that works.
If the latter (new project), it could be the data you're querying. Try query different data and see if it works. Usually yahoo throws back a specific error if something wrong, but it could be this.
If neither of those approaches work, but you still need to catch this sort of error, given the source code, what it does is:
if (!crumb) {
console.warn('root.Api.main context.dispatcher.stores.CrumbStore.crumb ' +
'structure no longer exists, please open an issue.');
And then continues on as normal (without throwing), and eventually returns an empty array.
If you're sure the result should contain at least one item, you can check to see if it's empty, and enter into an error state if it is.
Otherwise, if you don't know whether the array should contain values or not, another option is to overwrite console.warn so that you can detect when that exact string is passed to it.
Another option would be to fork the library so that it (properly) throws an error when not found, instead of continuing on and returning an empty array, making an empty successful result indistinguishable from an errored empty result. Change the
if (!crumb) {
console.warn('root.Api.main context.dispatcher.stores.CrumbStore.crumb ' +
'structure no longer exists, please open an issue.');
to
if (!crumb) {
throw new Error('root.Api.main context.dispatcher.stores.CrumbStore.crumb ' +
'structure no longer exists, please open an issue.');
and then you'll be able to catch it in your call to .historical.
I am new to Javascript but am attempting to build an Electron app as a gui to a python script.
I am using python-shell to call my Python script and can successfully do this and printing the output in the console. However I would like to capture multiple Python print statements in a Javascript array.
Following the python-shell docs I can access each Python print statement via the message event.
var res = [];
var ligation = PythonShell.run("ligation.py", options, function (err, results) {
if (err) throw err;
// console.log('results: %j', results);
});
ligation.on('message', function(message) {
// console.log(message)
res.push(message)
})
console.log(res)
However when I try to assign the ouput of these events to an array it works but I cannot access the values as the console looks like this:
where the 8 numbers are my output. When I try to access a single value (e.g. console.log(res[0])) I get it reported that it is undefined.
I gather that the little blue i means that the values are just evaluated and figure this may have something to do with it but I do not know what.
I figure there must be something straightforward I am missing. Any help is appreciated.
So basically, I have a bot that part of its functionality is to create channels inside of a guild (discord server.) I have the generic error handlers, and catch blocks, but what I am wondering is how to separate the error 'Maximum number of guild channels reached (500)' of opcode '30013,' so that if that is the error, it display a message saying the maximum amount of channels has been created, as well as allowing for the other errors to get caught.
Ideally all errors can still get caught, but if the error is the desired error, it will do what I ask, instead of simplifying the error JSON response, like the error handler make it do.
Something alone these lines should work if the error response received matches this link.
try {
// You code here
}
catch(err) {
if (err.code == 30013) {
// feedback code here
}
}
As I'm not sure if you are using async or not I'm presenting the standard try/catch format vs .catch. Regardless the logic should be the same.
Inspect the error object and see what property distinguishes it from others (most likely its code property). Once you know, check the property in your catch block against the desired one. If it matches, continue with your specific code.
I am trying to write a javascript file in express to talk to a postgresql database. More precisely, I want to write a function that takes SQL as an input parameter and returns the stringified json. I can assume memory is not an issue given these table sizes. This is paid work making an internal use tool for a private business.
My most recent attempt involved the query callback putting the value into a global variable, but even that still fails because the outermost function returns before the json string is defined. Here is the relevant code:
var dbjson;
function callDB(q) {
pg.connect(connectionString, function(err, client, done) {
if (err) {
console.error('error fetching client from pool', err);
} else {
client.query(q, [], function(err, result) {
client.query('COMMIT');
done();
if (err) {
console.error('error calling query ' + q, err);
} else {
dbjson = JSON.stringify(result.rows);
console.log('1 ' + dbjson);
}
console.log('2 ' + dbjson);
});
console.log('3 ' + dbjson);
}
console.log('4 ' + dbjson);
});
console.log('5 ' + dbjson);
}
The SQL in my test is "select id from users".
The relevant console output is:
5 undefined
GET /db/readTable?table=users 500 405.691 ms - 1671
3 undefined
4 undefined
1 [{"id":1},{"id":2},{"id":3},{"id":4}]
2 [{"id":1},{"id":2},{"id":3},{"id":4}]
Why do the console logs occur in the order that they do?
They are consistent in the order.
I attempted to write a polling loop to wait for the global variable to be set using setTimeout in the caller and then clearing the timeout within the callback but that failed, I think, because javascript is single threaded and my loop did not allow other activity to proceed. Perhaps I was doing that wrong.
While I know I could have each function handle its own database connection and error logging, I really hate repeating the same code.
What is a better way to do this?
I am relatively new to express and javascript but considerably more experienced with other languages.
Presence of the following line will break everything for you:
client.query('COMMIT');
You are trying to execute an asynchronous command in a synchronous manner, and you are calling done(), releasing the connection, before that query gets a chance to execute. The result of such invalid disconnection would be unpredictable, especially since you are not handling any error in that case.
And why are you calling a COMMIT there in the first place? That in itself looks completely invalid. COMMIT is used for closing the current transaction, that which you do not even open there, so it doesn't exist.
There is a bit of misunderstanding there in terms of asynchronous code usage and the database also. If you want to have a good start at both, I would suggest to have a look at pg-promise.
I using check to verify arguments to my meteor methods. I would like to send a helpful error message when the check fails.
Is there a way to specify a sanitized error to send to the client when the check fails?
I could wrap the check in a try/catch block and generate another Meteor error, but this seems needlessly verbose.
The only way to do it I figured so far is to put your check inside of try catch block and then throw new exception with reason/message taken from original one. Not perfect, even ugly, but works for now.
try {
check(param, String)
} catch (ex) {
let message = ex.message.startsWith('Match error: Match error: ')
? ex.message.slice(26, ex.message.length)
: ex.message
throw new Meteor.Error(400, message)
}