R/f_error_handling.R

Defines functions stopIncompleteArguments stopArgumentUnknown stopMissingArgument stopIndexOutOfBounds stopArgumentLengthOutOfBounds stopArgumentOutOfRange stopConflictingArguments stopIllegalDataInput stopIllegalArgument stopRuntimeIssue .stopWithError .createCondition .stripErrorExceptionType .getErrorCall .getErrorMessage

## |
## |  rpact: Confirmatory Adaptive Clinical Trial Design and Analysis
## |  This file is part of the rpact R package.
## |
## |  RPACT — Statistical Tools for Drug Development
## |
## |  Authors: Gernot Wassmer, PhD, and Friedrich Pahlke, PhD
## |  SPDX-FileCopyrightText: RPACT GmbH, Germany <info@rpact.com>
## |  SPDX-License-Identifier: LGPL-3.0-only
## |
## |  Licensed under the GNU Lesser General Public License, version 3.
## |  License text: https://www.gnu.org/licenses/lgpl-3.0.html
## |
## |  Package documentation: https://www.rpact.org
## |  Resources and customer portal: https://connect.rpact.app
## |  Consulting and services: https://www.rpact.com
## |  Contact: info@rpact.com
## |

#' @include f_core_constants.R
NULL

.getErrorMessage <- function(...) {
    return(paste0(unlist(list(...)), collapse = ""))
}

.getErrorCall <- function(call = NULL, asCharacter = TRUE) {
    if (isTRUE(asCharacter)) {
        return(sys.calls()[[1]])
    }

    if (!is.null(call)) {
        return(call)
    }

    if (sys.nframe() > 3L) {
        return(sys.call(-3L))
    }

    return(sys.call())
}

.stripErrorExceptionType <- function(message, exceptionType) {
    if (startsWith(message, exceptionType)) {
        return(substring(message, nchar(exceptionType) + 1L))
    }

    return(message)
}

.createCondition <- function(
        message,
        ...,
        factoryKey,
        code = NULL,
        category = NULL,
        class = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    factory <- getOption(factoryKey)
    if (!is.function(factory)) {
        factory <- getOption("rpact.error.factory.condition")
    }

    if (is.function(factory)) {
        condition <- tryCatch(
            {
                # Omit absent optional fields for compatibility with existing factories.
                # Quote language-valued arguments so diagnostic calls are not evaluated.
                factoryArguments <- list(
                    message = message, ..., code = code, category = category,
                    call = call, class = class
                )
                if (!is.null(diagnosticId)) {
                    factoryArguments$diagnosticId <- diagnosticId
                }
                if (!is.null(context)) {
                    factoryArguments$context <- context
                }
                do.call(factory, factoryArguments, quote = TRUE)
            },
            error = function(e) {
                message("Error in condition factory: ", e$message)
                return(NULL)
            }
        )
        if (inherits(condition, "condition")) {
            return(condition)
        }
    }

    condition <- simpleError(message, call = NULL)
    class(condition) <- unique(c(class, class(condition)))
    return(condition)
}

.stopWithError <- function(
        message,
        ...,
        exceptionType,
        factoryKey,
        code,
        category,
        class,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    message <- .stripErrorExceptionType(message, exceptionType)
    condition <- .createCondition(
        message = paste0(exceptionType, message),
        ...,
        factoryKey = factoryKey,
        code = code,
        category = category,
        class = class,
        call = call,
        diagnosticId = diagnosticId,
        context = context
    )
    stop(condition)
}

#'
#' @examples
#' stopRuntimeIssue("An error occurred")
#'
#' @noRd
#'
stopRuntimeIssue <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_RUNTIME_ISSUE,
        factoryKey = "rpact.error.factory.runtime.issue.condition",
        code = "RUNTIME_ISSUE",
        category = "runtime_issue",
        class = c("rpact_runtime_issue_error", "rpact_error"),
        call = call
    )
}

#'
#' @examples
#' stopIllegalArgument("An error occurred")
#'
#' @noRd
#'
stopIllegalArgument <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_ILLEGAL_ARGUMENT,
        factoryKey = "rpact.error.factory.illegal.argument.condition",
        code = "ILLEGAL_ARGUMENT",
        category = "invalid_user_input",
        class = c("rpact_illegal_argument_error", "rpact_input_error"),
        call = call
    )
}

stopIllegalDataInput <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_ILLEGAL_DATA_INPUT,
        factoryKey = "rpact.error.factory.illegal.data.input.condition",
        code = "ILLEGAL_DATA_INPUT",
        category = "invalid_data_input",
        class = c("rpact_illegal_data_input_error", "rpact_input_error"),
        call = call
    )
}

stopConflictingArguments <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_CONFLICTING_ARGUMENTS,
        factoryKey = "rpact.error.factory.conflicting.arguments.condition",
        code = "CONFLICTING_ARGUMENTS",
        category = "invalid_user_input",
        class = c("rpact_conflicting_arguments_error", "rpact_input_error"),
        call = call
    )
}

stopArgumentOutOfRange <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        lowerBound = NULL,
        upperBound = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        lowerBound = lowerBound,
        upperBound = upperBound,
        exceptionType = C_EXCEPTION_TYPE_ARGUMENT_OUT_OF_RANGE,
        factoryKey = "rpact.error.factory.argument.out.of.range.condition",
        code = "ARGUMENT_OUT_OF_RANGE",
        category = "invalid_user_input",
        class = c("rpact_argument_out_of_range_error", "rpact_input_error"),
        call = call
    )
}

stopArgumentLengthOutOfBounds <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        lowerBound = NULL,
        upperBound = NULL,
        expectedLength = NULL,
        actualLength = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        lowerBound = lowerBound,
        upperBound = upperBound,
        expectedLength = expectedLength,
        actualLength = actualLength,
        exceptionType = C_EXCEPTION_TYPE_ARGUMENT_LENGTH_OUT_OF_RANGE,
        factoryKey = "rpact.error.factory.argument.length.out.of.range.condition",
        code = "ARGUMENT_LENGTH_OUT_OF_RANGE",
        category = "invalid_user_input",
        class = c("rpact_argument_length_out_of_range_error", "rpact_input_error"),
        call = call
    )
}

stopIndexOutOfBounds <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        lowerBound = NULL,
        upperBound = NULL,
        index = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        lowerBound = lowerBound,
        upperBound = upperBound,
        index = index,
        exceptionType = C_EXCEPTION_TYPE_INDEX_OUT_OF_BOUNDS,
        factoryKey = "rpact.error.factory.index.out.of.bounds.condition",
        code = "INDEX_OUT_OF_BOUNDS",
        category = "invalid_user_input",
        class = c("rpact_index_out_of_bounds_error", "rpact_input_error"),
        call = call
    )
}

stopMissingArgument <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_MISSING_ARGUMENT,
        factoryKey = "rpact.error.factory.missing.argument.condition",
        code = "MISSING_ARGUMENT",
        category = "invalid_user_input",
        class = c("rpact_missing_argument_error", "rpact_input_error"),
        call = call
    )
}

stopArgumentUnknown <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        exceptionType = C_EXCEPTION_TYPE_UNKNOWN_ARGUMENT,
        factoryKey = "rpact.error.factory.unknown.argument.condition",
        code = "UNKNOWN_ARGUMENT",
        category = "invalid_user_input",
        class = c("rpact_unknown_argument_error", "rpact_input_error"),
        call = call
    )
}

stopIncompleteArguments <- function(
        ...,
        parameter = NULL,
        value = NULL,
        constraint = NULL,
        functionName = NULL,
        relatedParameter = NULL,
        relatedValue = NULL,
        missingParameters = NULL,
        definedParameters = NULL,
        call = NULL,
        diagnosticId = NULL,
        context = NULL) {
    call <- .getErrorCall(call = call)
    message <- .getErrorMessage(...)
    .stopWithError(
        message,
        functionName = functionName,
        parameter = parameter,
        value = value,
        constraint = constraint,
        relatedParameter = relatedParameter,
        relatedValue = relatedValue,
        diagnosticId = diagnosticId,
        context = context,
        missingParameters = missingParameters,
        definedParameters = definedParameters,
        exceptionType = C_EXCEPTION_TYPE_INCOMPLETE_ARGUMENTS,
        factoryKey = "rpact.error.factory.incomplete.arguments.condition",
        code = "INCOMPLETE_ARGUMENTS",
        category = "invalid_user_input",
        class = c("rpact_incomplete_arguments_error", "rpact_input_error"),
        call = call
    )
}

Try the rpact package in your browser

Any scripts or data that you put into this service are public.

rpact documentation built on Nov. 17, 2026, 1:07 a.m.