Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silence error messages #70

Closed
R2Python opened this issue Dec 9, 2021 · 2 comments
Closed

Silence error messages #70

R2Python opened this issue Dec 9, 2021 · 2 comments

Comments

@R2Python
Copy link

R2Python commented Dec 9, 2021

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
Copy link
Owner

aryoda commented Dec 10, 2021

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
Copy link
Owner

aryoda commented Jul 28, 2022

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

@aryoda aryoda closed this as completed Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants