aryoda / tryCatchLog

R package for better error handling incl. logging with a full (!) stack trace incl. line numbers, post-mortem analysis and support for parallel processing
Other
73 stars 8 forks source link

Silence error messages #70

Closed R2Python closed 2 years ago

R2Python commented 2 years ago

In some situations, it would be helpful to suppress error messages. Examples include:

https://stackoverflow.com/questions/19111956/suppress-error-message-in-r https://stackoverflow.com/questions/70274274/r-errorhandling-with-trycatchlog-create-a-customizable-result-code-for-the-co

According to the documentation "Warnings and messages can be “silenced” (only logged but not propagated to the caller) using the silent.* parameters.", implying that errors can not be silenced.

There is also an option to create dump files in case of errors, but this is also not suppressing error messages. Thus, the following code is not suppressing error warnings:

parameter_check_1 <- function(error) {
  if (error){
    stop()
  }
}

tryCatchLog(parameter_check_1(error = TRUE),
  error = function(e){} , # do nothing - in tryCatch this would suppress the error message according to https://stackoverflow.com/a/48831338/7219311
  write.error.dump.file = getOption("tryCatchLog.write.error.dump.file", FALSE),
  silent.warnings       = getOption("tryCatchLog.silent.warnings", FALSE),
  silent.messages       = getOption("tryCatchLog.silent.messages", FALSE))

It would be helpful to add a silent.errors option.

aryoda commented 2 years ago

Thanks for your valuable feed back to improve tryCatchLog!

Could you please help me to understand your feature request more exactly?

it would be helpful to suppress error messages

Do you want to a) suppress the logging of error messages or b) suppress the thrown error without handling it (to continue the execution with the next command after tryCatchLog())? This is what your example code does.

Regarding a): There is already a similar open feature request (but only for warnings): #11

Regarding b): I guess tryCatchLog::tryLog() could work then.

Historically in base R there is no "silent errors" argument in tryCatch() because the reason to use this function is exactly to catch (and handle) errors. If you do not want to catch and handle errors, base R provides try() which does not throw an error but just returns an "error" object instead.

In tryCatchLog I have kept this base R semantics and "just" added logging for easy understanding ("don't surprise the user with unexpected semantics").

aryoda commented 2 years ago

There is no ways auf muffling ("silencing" in tryCatchLog wording) an error condition:

Errors are not mufflable since they are signalled in critical situations where execution cannot continue safely

Source: https://rlang.r-lib.org/reference/cnd_muffle.html

I am afraid I have to close this issue (subject to be reopened when base R should support this one day)...