% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/errorHandling.R
\name{stopOnError}
\alias{stopOnError}
\title{Stop on try-error}
\usage{
stopOnError(
  result,
  err_msg = "",
  prefix = "",
  maxLength = 500,
  truncatedLength = 120,
  call. = FALSE
)
}
\arguments{
\item{result}{Return value from a \code{try()} block.}

\item{err_msg}{Optional custom error message.}

\item{prefix}{Optional text to prepend to the error message.}

\item{maxLength}{Maximum allowed error message length before truncation.}

\item{truncatedLength}{Length of the truncated error message.}

\item{call.}{Logical indicating whether the call should be included in the
error message. Passed to \code{\link[=stop]{stop()}}.}
}
\value{
Returns \code{NULL} if \code{result} is not a \code{"try-error"}; otherwise stops with an
error.
}
\description{
Generate a consistent error message from the result of a \code{try()} block.
}
\details{
This function is intended for production code where potentially fragile
operations are wrapped in \code{try(..., silent = TRUE)}. If \code{result} inherits
from \code{"try-error"}, a cleaned and optionally customized error message is
generated and passed to \code{\link[=stop]{stop()}}.

If \code{result} is not a \code{"try-error"}, the function returns \code{NULL}.
}
\note{
If logging has been initialized, the final error message is logged with
\code{\link[=logger.error]{logger.error()}} before calling \code{\link[=stop]{stop()}}.
}
\examples{
\dontrun{
myFunc <- function(x) {
  log(x)
}

result <- try({
  myFunc("ten")
}, silent = TRUE)

stopOnError(result)

try({
  myFunc("ten")
}, silent = TRUE) \%>\%
  stopOnError(err_msg = "Unable to process user input")

try({
  myFunc("ten")
}, silent = TRUE) \%>\%
  stopOnError(
    prefix = "USER_INPUT_ERROR",
    maxLength = 40,
    truncatedLength = 32
  )
}

}
