How to handle errors from reqMktData calls - error-handling

Are there any examples on the net how to process errors when downloading data from Interactive Brokers using the IBrokers package? I've had a look at the package details and eWrapper and twsCALLBACK seem to handle this but I can't get them to work. For example the code below produces an error and R hangs, the error msg isn't processed. Thanks for any suggestions.
contract <- twsContract(0,
symbol="SPI",
sectype="XXX", #bad sectype
exch="SNFE",
primary="",
expiry= "20181220",
strike="",
currency="AUD",
right="",
local="",
multiplier = "25",
combo_legs_desc = "",
comboleg = "",
include_expired = "",
secIdType = "",
secId = "")
tws <- twsConnect()
data <- reqMktData(tws,contract,snapshot = TRUE)

You should append a "Disconnect" command to you code. Otherwise your program try to build to connections on the same port, that's not possible and it will not terminate.
I don't know the IBroker package very well, please check the command for disconnecting and append it to your code. Refresh your command line and rerun your code.
In addition, connect to IB Gateway instead of TWS by using that port number (check API settings of your IB Gateway application). In the settings choose a detailed Log.
Run your code again (after changing port number) and send your log file. Then I will try to help more. It's hard to help without any error message.

Related

Why isn't Entrez NCBI API error handling working?

from Bio import Entrez, __version__
print('Biopython version : ', __version__)
id_list = ["NC_045512.2", "ON248099.1", "ON248101.1", "ON248104.1", "ON248107.1", "ON248108.1", "ON248109.1", "ON248110.1", "ON248114.1", "ON247234.1", "ON247236.1", "ON247240.1", "ON247242.1", "ON247243.1", "ON247244.1", "ON247245.1", "ON247246.1", "ON247247.1", "ON247248.1"]
try:
print ("Downloading "+str(id_list[ax]))
net_handle = Entrez.efetch(db="nucleotide", id=id_list[ax], rettype="fasta", retmode="text")
net_handle_gb = Entrez.efetch(db="nucleotide", id=id_list[ax], rettype="gbwithparts", retmode="text")
except (IOError):
print ("Waiting")
sleep (30)
tell.app( 'Terminal', 'do script "' + command+ ' && ' + "python3 'All Covid SSR.py'" +'"')
I'm using this try block to download files from NCBI API. Up to this point, the try block was working and the exception was handled as given here. Now it isn't. The code gets stuck in the try section. Returns no errors or exceptions, so it doesn't trigger the exception handler.
I've set max_tries = 1, sleep_between_tries = 1 and got the personal API_Key too.
For context, I'm using the exceptions block to restart the downloading code on Mac Terminal.
I need the try and except section to automatically restart the script when Entrez.efetch returns an error. There isn't any error. The code just gets stuck after
print ("Downloading "+str(id_list[ax]))
Any suggestions are welcome.
P.S. I thought of changing the default timeout for requests.get because that's the module Entrez uses to access the NCBI API. Haven't found a way to do that. (There is no timeout variable that you can set in the efetch module.)
An explanation would be great!

How do I handle session id context uninitialized in Thrift

sorry to bother you guys,
I have a Thrift server program in c++. Whenever a client connects to me, the handshake succeeds, as does the first Thrift command sent, but the second command sent fails with the error "session id context uninitialized".
The client's next command re-establishes the connection and succeeds, but the fourth will fail again with "session id context uninitialized".
The exact error is
TConnectedClient died: SSL_accept: session id context uninitialized (SSL_error_code = 1)
TConnectedClient input close failed: session id context uninitialized (SSL_error_code = 1)
TConnectedClient output close failed: session id context uninitialized (SSL_error_code = 1)
every 'even' command
My problem seems similar to THIS, but I can't seem to figure out how to change the context of my session to set the SSL_OP_NO_TICKET flag.
I tried adding a ServerEventHandler, but I don't think I can change the serverContext that is present there.
Can anyone help me?
Below is the section of main() where I declare and start the server. If more information is needed, please ask. (sorry if I typo-ed any code, I had to retype by hand it here)
::apache::thrift::stdcxx::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactoryT<TBufferBase>());
::apache::thrift::stdcxx::shared_ptr<My_svrHandler> handler(new My_svrHandler());
::apache::thrift::stdcxx::shared_ptr<TProcessor> processor(new My_svrProcessor(handler));
::apache::thrft::stdcxx::shared_ptr<TSSLSocketFactory> sslSocketFactory(new TSSLSocketFactory(SSL::TLSv1_2));
sslSocketFactory->loadCertificate(certLocation);
sslSocketFactory->loadPrivateKey(keyLocation);
sslSocketFactory->loadTrustedCertificates(CALocation);
sslSocketFactory->authenticate(true);
::apache::thrift::stdcxx::shared_ptr<TServerSocket> serverSocket(new TSSLServerSocket(9090, sslSocketFactory));
::apache::thrift::stdcxx::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
::apache::thrift::stdcxx::shared_ptr<apache::thrift::server::Tserver> server;
::apache::thrift::stdcxx::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(10);
::apache::thrift::stdcxx::shared_ptr<PlatformThreadFactory> threadFactory = ::apache::thrift::stdcxx::shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
server.reset(new TThreadedPoolServer(processor, serverSocket, transportFactory, protocolFactory, threadmanager));
if(server.get() != NULL)
{
apache::thrift::concurrency::PlatformThreadFactory factory;
::apache::thrift::stdcxx::shared_ptr<apache::thrift::concurrency::Runnable> serverThreadRunner(server);
::apache::thrift::stdcxx::shared_ptr<apache::thrift::concurrency::Thread> thread = factory.newThread(serverThreadRunner);
signal(SIGPIPE, SIG_IGN);
thread->start();
while(1){}
server->stop();
thread->join();
server.reset();
}

Excel power query, error handling for fallback connection/OleDb data source

I have a PowerQuery connected to DB2 Data Source, but because of some kind of load balancing the DB Server changes periodically (ip also changes), There is no way for me to know beforehand which data source (IP), I should use until I try it and see that it errors out, then I have to use the other one, I googled for error handling in PowerQuery and found some examples of error handling but this examples did not apply to my case, most of them handled errors AFTER the connection was made or were preventing errors on missing columns or files not found, I tried to adjust the examples to my case but was not able to.
What I want is just try one IP and if it fails then use the other one.
let
//fParametros("ParamQuery",1) is the "standard" Server/Ip address (provider=IBMDADB2.IBMDBCL1;data source=CP3;location=pn8us7ldbcp3.us.mycompany.com:5912)
dbSource = fParametros("ParamQuery",1),
//fParametros("ParamQuery",5) is the "Alternate" Server/Ip address (provider=IBMDADB2.IBMDBCL1;data source=CP3;location=pn8us7ldbcp3h.us.mycompany.com:5912)
AltdbSource = fParametros("ParamQuery",5),
pOrden = Text.From(fParametros("ParamQuery",2)),
//Create the query
dbQuery = "SELECT SAPCP3.vbak.VBELN SO , SAPCP3.vbap.posnr PoLine , SAPCP3.vbep.ETENR Sch_Line , SAPCP3.vbap.matnr Part_Number,SAPCP3.makt.maktx Description,SAPCP3.vbap.kwmeng Qty ,SAPCP3.vbep.BMENG Conf_qty ,SAPCP3.vbap.vrkme UOM ,SAPCP3.vbap.netpr SalesPrice ,SAPCP3.vbap.kpein LotSize FROM SAPCP3.vbak JOIN SAPCP3.vbap ON SAPCP3.VBAp.VBELN = SAPCP3.VBAK.VBELN JOIN SAPCP3.vbep ON SAPCP3.vbep.vbeln = SAPCP3.vbak.vbeln AND SAPCP3.vbap.posnr = SAPCP3.vbep.posnr JOIN sapcp3.makt ON sapcp3.vbap.matnr=sapcp3.makt.matnr WHERE SAPCP3.VBAK.VKORG = '4000' AND (SAPCP3.vbep.edatu >= '20190701') AND SAPCP3.vbak.VBELN ="& pOrden & " ORDER BY SAPCP3.vbak.VBELN",
//Get the data
Source = OleDb.DataSource(dbSource, [Query=dbQuery]),
//Failed Attempt to handle the error:
TestForError= try Source,
//next line does not work, I get error saying Source is already defined/declared
Source = if TestForError[HasError] then OleDb.DataSource(AltdbSource, [Query=dbQuery]) else OleDb.DataSource(dbSource, [Query=dbQuery])
in
Source
I also Tried the following:
.
.
.
//Get the data
Source = OleDb.DataSource(dbSource, [Query=dbQuery]),
//Failed Attempt to handle the error:
TestForError= try Source,
Output = if TestForError[HasError] then OleDb.DataSource(AltdbSource, [Query=dbQuery]) else OleDb.DataSource(dbSource, [Query=dbQuery])
in
Output
//This last part works if the dbSource is correct, but if it is not it doesnt catch the error and gives me the Connection error shown below:
DataSource.Error: OLE DB: SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "172.16.0.1". Communication function detecting the error: "connect". Protocol specific error code(s): "10061", "", "". SQLSTATE=08001
Details:
DataSourceKind=OleDb
DataSourcePath=data source=CP3;location=pn8us7ldbcp3.us.mycompany.com:5912;provider=IBMDADB2.IBMDBCL1
Message= SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "172.16.0.1". Communication function detecting the error: "connect". Protocol specific error code(s): "10061", "", "". SQLSTATE=08001
ErrorCode=-2147467259
I want to connect to dbSource (172.16.0.1) and if it is not possible then connect to AltdbSource (172.16.0.2)
Currently I have 2 identical excel sheeets and user needs to open one or the other if he gets the connection error, I want it to change automatically.
I'm guessing that TestForError = try OleDb.DataSource(BadSource) isn't working because OleDb.DataSource(...) returns a table value than only produces an error when trying to enumerate rows.
What if you change TestForError to drill into the first row if it exists:
TestForError= try Source{0}?,
If that still doesn't work, next try indexing into a table cell in the first row.

WebSphere wsadmin testConnection error message

I'm trying to write a script to test all DataSources of a WebSphere Cell/Node/Cluster. While this is possible from the Admin Console a script is better for certain audiences.
So I found the following article from IBM https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/txml_testconnection.html which looks promising as it describles exactly what I need.
After having a basic script like:
ds_ids = AdminConfig.list("DataSource").splitlines()
for ds_id in ds_ids:
AdminControl.testConnection(ds_id)
I experienced some undocumented behavior. Contrary to the article above the testConnection function does not always return a String, but may also throw a exception.
So I simply use a try-catch block:
try:
AdminControl.testConnection(ds_id)
except: # it actually is a com.ibm.ws.scripting.ScriptingException
exc_type, exc_value, exc_traceback = sys.exc_info()
now when I print the exc_value this is what one gets:
com.ibm.ws.scripting.ScriptingException: com.ibm.websphere.management.exception.AdminException: javax.management.MBeanException: Exception thrown in RequiredModelMBean while trying to invoke operation testConnection
Now this error message is always the same no matter what's wrong. I tested authentication errors, missing WebSphere Variables and missing driver classes.
While the Admin Console prints reasonable messages, the script keeps printing the same meaningless message.
The very weird thing is, as long as I don't catch the exception and the script just exits by error, a descriptive error message is shown.
Accessing the Java-Exceptions cause exc_value.getCause() gives None.
I've also had a look at the DataSource MBeans, but as they only exist if the servers are started, I quickly gave up on them.
I hope someone knows how to access the error messages I see when not catching the Exception.
thanks in advance
After all the research and testing AdminControl seems to be nothing more than a convinience facade to some of the commonly used MBeans.
So I tried issuing the Test Connection Service (like in the java example here https://www.ibm.com/support/knowledgecenter/en/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/cdat_testcon.html
) directly:
ds_id = AdminConfig.list("DataSource").splitlines()[0]
# other queries may be 'process=server1' or 'process=dmgr'
ds_cfg_helpers = __wat.AdminControl.queryNames("WebSphere:process=nodeagent,type=DataSourceCfgHelper,*").splitlines()
try:
# invoke MBean method directly
warning_cnt = __wat.AdminControl.invoke(ds_cfg_helpers[0], "testConnection", ds_id)
if warning_cnt == "0":
print = "success"
else:
print "%s warning(s)" % warning_cnt
except ScriptingException as exc:
# get to the root of all evil ignoring exception wrappers
exc_cause = exc
while exc_cause.getCause():
exc_cause = exc_cause.getCause()
print exc_cause
This works the way I hoped for. The downside is that the code gets much more complicated if one needs to test DataSources that are defined on all kinds of scopes (Cell/Node/Cluster/Server/Application).
I don't need this so I left it out, but I still hope the example is useful to others too.

VBA Error - Reflection.FTP.3

I had this error pop up on exactly the 3rd line of code below. There seem to be no explanation on the Internet for this behaviour.
I'm looking at why this error came up, and fixed itself after few minutes.
Set Ftp = CreateObject("Reflection.FTP.3")
Ftp.Open "xxx.xxx.xxx.xxx", "username", "password"
Ftp.SetCurrentDirectory "DirectoryName/DirectoryName/DirectoryName"
What was the error?
Run-time error '-2147418113 (8000ffff)':
Method 'SetCurrentDirectory' of object 'IReflectionFTP' failed
More details:
Application: Excel Macro
Language : VB (VBA)
*Is this because of a coding error? *
Not likely. The macro has been long running and this came up for the first time.
*Is it because of a FTP service disruption? *
May be. But logs have a recording for every second and there seems to be no outage.
It seems to me there is a connection problem here - maybe a timeout? I assume that your three lines of code don't execute one after another (ie the SetCurrentDirectory is after some more code). This error will come up if the Ftp object doesn't have a valid connection that is logged in. Change the IP for the Open command to an invalid one and you'll see you get the same error.
Try setting the following line of code before SetCurrentDirectory command.
If FTP.Status = rcLoggedIn + rcConnected Then
Ftp.SetCurrentDirectory "DirectoryName/DirectoryName/DirectoryName"
Else
'Error handle
End If
Note, that you are late binding the object so for it to work for you, you'll need the If statement to be:
If FTP.Status = 17 Then
Also, if it is a timeout problem then I'd set the Timeout period for the session to be longer, ie FTP.TimeoutSession = 300.