File: ErrorHandling.R

package info (click to toggle)
r-bioc-biocparallel 1.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,768 kB
  • sloc: cpp: 139; sh: 14; makefile: 8
file content (145 lines) | stat: -rw-r--r-- 3,907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
### =========================================================================
### Error handling
### -------------------------------------------------------------------------
.bpeltok <-
    function(x, type = bperrorTypes())
{
    !inherits(x, type)
}

bpok <-
    function(x, type = bperrorTypes())
{
    x <- bpresult(x)
    type <- match.arg(type)
    vapply(x, .bpeltok, logical(1), type)
}

.bpallok <-
    function(x, type = bperrorTypes(), attrOnly = FALSE)
{
    if (attrOnly)
        is.null(.redo_env(x))
    else
        is.null(.redo_env(x)) && all(bpok(x, type))
}

bptry <-
    function(expr, ..., bplist_error, bperror)
{
    if (missing(bplist_error))
        bplist_error <- bpresult

    if (missing(bperror))
        bperror <- identity

    tryCatch(expr, ..., bplist_error=bplist_error, bperror=bperror)
}

bpresult <- function(x)
{
    if (is(x, "bplist_error"))
        x <- attr(x, "result")
    x
}

.error <- function(msg, class=NULL) {
    structure(list(message=msg),
              class = c(class, "bperror", "error", "condition"))
}

.error_remote <- function(x, call) {
    structure(x, class = c("remote_error", "bperror", "error", "condition"),
              traceback = capture.output(traceback(call)))
}

.error_unevaluated <- function()
{
    structure(list(message="not evaluated due to previous error"),
              class=c("unevaluated_error", "bperror", "error", "condition"))
}

.error_not_available <- function(msg)
{
    structure(list(message=msg),
              class=c("not_available_error", "bperror", "error", "condition"))
}

.error_worker_comm <- function(error, msg) {
    msg <- sprintf("%s:\n  %s", msg, conditionMessage(error))
    structure(list(message=msg, original_error_class=class(error)),
              class=c("worker_comm_error", "bperror", "error", "condition"))
}

bperrorTypes <-
    function()
{
    subclasses <- paste0(
        c("remote", "unevaluated", "not_available", "worker_comm"),
        "_error"
    )
    c("bperror", subclasses)
}

.error_bplist <- function(result) {
    if (is.null(attr(result, "errors"))) {
        errors <- result
        total_error <- sum(!bpok(errors))
        remote_error <-
            !bpok(errors, "remote_error") |
            !bpok(errors, "worker_comm_error")
        remote_idx <- which(remote_error)
        if (length(remote_idx))
          first_error <- errors[[remote_idx[1]]]
        else
          first_error <- ""
    } else {
        errors <- attr(result, "errors")
        total_error <- length(errors)
        remote_error <-
            !bpok(errors, "remote_error") |
            !bpok(errors, "worker_comm_error")
        first_error_idx <- which(remote_error)[1]
        if (!is.null(first_error_idx))
          first_error <- errors[[first_error_idx]]
        else
          first_error <- ""
        remote_idx <- as.integer(names(errors[remote_error]))
    }

    n_remote_error <- length(remote_idx)
    n_other_error <- total_error - n_remote_error

    fmt = paste(
        "BiocParallel errors",
        "%d remote errors, element index: %s%s",
        "%d unevaluated and other errors",
        "first remote error:\n%s",
        sep = "\n  "
    )
    class(first_error) <- tail(class(first_error), 2L)
    first_error_msg <- as.character(first_error)
    message <- sprintf(
        fmt,
        n_remote_error,
        paste(head(remote_idx), collapse = ", "),
        ifelse(length(remote_idx) > 6, ", ...", ""),
        n_other_error,
        first_error_msg
    )

    err <- structure(
        list(message=message),
        result=result,
        class = c("bplist_error", "bperror", "error", "condition"))
}

print.remote_error <- function(x, ...) {
    NextMethod(x)
    cat("traceback() available as 'attr(x, \"traceback\")'\n")
}

`print.bplist_error` <- function(x, ...) {
    NextMethod(x)
    cat("results and errors available as 'bpresult(x)'\n")
}