I am new to R. Kindly excuse me if it is a basic query. I am facing a strange situation where I am using try catch to catch errors and warnings while sourcing another script. It is working fine also. However, in one particular case, while sourcing, there is an error message followed by a warning message, but try catch in my script is considering it only as a warning message.
Please find my code as follows
result1 <- tryCatch ({
source("Path with script.R")
inc_val = 1
}, error = function(er) {
inc_val = 2
} , warning = function(er) {
inc_val = 3
},finally = {
})
So in this particular case, result1 is 3 instead of 2, even though there is an error while executing the script.
Kindly let me know where I have done the mistake.
Thanks in advance.
Related
I have a code block, to perform 3 times retry of the code execution in case of a specific error. In below example if HTTP 503 error occurred during the data download from ADLS container, I want the same operation to be executed maximum of 3 times retry.
require(AzureStor)
require(stringr)
recheck <- 0
while (recheck < 3){
recheck <- recheck + 1
tryCatch({
storage_download(container, file, filename, overwrite=TRUE)
recheck <- 4
}, error = function(e){
if ( sum(str_detect(e, '503')*1) > 0 ){
print(e)
print(paste0('An infra-level failure occured. Retry sequence number is : ', recheck))
} else{
recheck <<- 4
print(e)
}
}
)
}
This code works fine for me, but similar to storage_download in the above example, I have other ADLS operations like delete_blob, upload_blob, storage_upload, list_storage_files at multiple instances in the code, I have to write above mentioned code for each of these functions. I want to make the above code as a function which can be called during each of these ADLS operations. Any thoughts or suggestions would help me greatly.
The following should do the trick:
with_retries_on_failure = function (expr, retries = 3L) {
expr = substitute(expr)
for (try in seq_len(retries)) {
tryCatch(
return(eval.parent(expr)),
error = \(e) {
if (str_detect(conditionMessage(e), '503')) stop(e)
message('An infra-level failure occurred. Retry sequence number is: ', try)
}
)
}
}
Used as follows:
with_retries_on_failure(storage_download(container, file, filename, overwrite=TRUE))
Note the return() call, which immediately returns from the surrounding function without the need to update the loop variable. Likewise, in the case of a failure we also don’t have to update the loop variable since we are using a for loop, and we use stop() to break out of the loop for any error that is not a 503 HTTP response.
This report has been running fine and all of a sudden is giving the below error message
A BIRT exception occurred. See next exception for more information.
TypeError: Cannot find function getLong in object com.ibm.tivoli.maximo.report.script.MXReportDataSetImpl#4188e92b. (/report/data- sets/script-data-set[#id="1990"]/method[#name="fetch"]#29).
at org.eclipse.birt.report.engine.script.internal.DtEScriptExecutor.handleJS (DtEScriptExecutor.java:99)
at org.eclipse.birt.report.engine.script.internal.DataSetScriptExecutor.handleJS(DataSetScriptExecutor.java:256)
at org.eclipse.birt.report.engine.script.internal.ScriptDataSetScriptExecutor.handleFetch(ScriptDataSetScriptExecutor.java:143)
at org.eclipse.birt.data.engine.impl.ScriptDataSetRuntime.fetch(ScriptDataSetRuntime.java:103)
at org.eclipse.birt.data.engine.impl.PreparedScriptDSQuery$ScriptDSQueryExecutor$CustomDataSet.fetch(PreparedScriptDSQuery.java:260)
at org.eclipse.birt.data.engine.executor.cache.OdiAdapter.fetch(OdiAdapter.java:226)
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.fetch(RowResultSet.java:145)
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.doNext(RowResultSet.java:118)
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.next(RowResultSet.java:96)
at org.eclipse.birt.data.engine.executor.cache.ExpandableRowResultSet.next(ExpandableRowResultSet.java:63)
at org.eclipse.birt.data.engine.executor.cache.SmartCacheHelper.populateData(SmartCacheHelper.java:318)
Below is the code which has been written in FETCH method that's causing the error. If I change getLong to getString then it works without any issue. However I would like to understand what could cause the issue since it was working fine before without any issue.
commLogDataSet = MXReportDataSetProvider.create(this.getDataSource().getName(), "commLogDataSet");
commLogDataSet.open();
commLogSQL = "select commlog.message, commlog.ownerid
from commlog where commlog.commlogid = " + workLogDataSet.getDouble("worklogid")
;
commLogDataSet.setQuery(commLogSQL);
if(commLogDataSet.fetch()) {
row["recordkey"] = commLogDataSet.**getLong**("ownerid");
row["description"] = commLogDataSet.getString("message");
}
commLogDataSet.close();
}
I have a scenario where I want to check If R has loaded the .RDA(which is a model)
I want this for getting prediction call as I don`t want to load every time I am asking for a prediction.
I tried with this below code
if(!is.na(T2I_Vendor_Eval1.rda)){
print("started")
bar<-load(file = "C:\\T2I_Vendor_Eval1.rda")
print("ended ")
}
Result I get is
Error: object 'T2I_Vendor_Eval1.rda' not found
Instead of doing this
if(!is.na(T2I_Vendor_Eval1.rda)){
print("started")
bar<-load(file = "C:\\T2I_Vendor_Eval1.rda")
print("ended end")
}
I did this
if(!exists("T2I_Vendor_Eval1")){
print("started")
load(file = "C:\\T2I_Vendor_Eval1.rda")
print("ended end")
}
It worked for me.
thanks for you help #JonGrub
So this is a strange one. My code does a bunch a things that are hard to explain (but if necessary I´ll try to explain), but the following works:
var res = data.delete_if (function(key, value) { return key == "a"; })
but the following crashes:
data.delete_if (function(key, value) { return key == "a"; })
So, the fact that I do not save the result of the delete_if function crashes the browser with the following stack trace:
Error: test: B environment should proxy a Ruby hash. (MDArraySolTest): Java::JavaLang::IllegalStateException: Channel stream was closed before response has been received.
java.lang.reflect.Method.invoke(java/lang/reflect/Method.java:498) org.jruby.javasupport.JavaMethod.invokeDirectWithExceptionHandling(org/jruby/javasupport/JavaMethod.java:453)
Any ideas of why this happens? Any solutions? I can provide more information if needed.
EDIT1:
Doing some more tests I found out that the error occurs only if the call to data.delete_if is the last statement on the script. If I add for example: console.log(""); after the call, everything works fine.
Thanks
I have a list of URL named URLlist and I loop over it to get the source code for each of those URL :
for (k in 1:length(URLlist)){
temp = getURL(URLlist[k])
}
Problem is for some random URL, the code get stuck and I get the error message:
Error in function (type, msg, asError = TRUE) :
transfer closed with outstanding read data remaining
But when I try the getURL function, not in the loop, with the URL which had a problem, it perfectly works.
Any help please ? thank you very much
Hard to tell for sure without more information, but it could just be the requests getting sent too quickly, in which case just pausing between requests could help :
for (k in 1:length (URLlist)) {
temp = getURL (URLlist[k])
Sys.sleep (0.2)
}
I'm assuming that your actual code does something with 'temp' before writing over it in every iteration of the loop, and whatever it does is very fast.
You could also try building in some error handling so that one problem doesn't kill the whole thing. Here's a crude example that tries twice on each URL before giving up:
for (url in URLlist) {
temp = try (getURL (url))
if (class (temp) == "try-error") {
temp = try (getURL (url))
if (class (temp) == "try-error")
temp = paste ("error accessing", url)
}
Sys.sleep(0.2)
}