@@ -100,7 +100,16 @@ prepare_inputs <- function(fn, par_inits, n_pars, extra_args_list, grad_fun, low
100100 call. = FALSE )
101101 }
102102
103- fn1 <- function (v ) { do.call(fn , c(list (v ), extra_args_list )) }
103+ fn1 <- function (v ) {
104+ # Catch errors in user function and return -Inf with message as attribute
105+ tryCatch({
106+ do.call(fn , c(list (v ), extra_args_list ))
107+ }, error = function (e ) {
108+ res <- - Inf
109+ attr(res , " message" ) <- e $ message
110+ res
111+ })
112+ }
104113 for (chain in seq_len(num_chains )) {
105114 validate_function(fn1 , inits [[chain ]], extra_args_list , grad = FALSE )
106115 }
@@ -121,7 +130,16 @@ prepare_inputs <- function(fn, par_inits, n_pars, extra_args_list, grad_fun, low
121130 fun_packages <- c(gp $ packages , packages )
122131 }
123132 if (! is.null(grad_fun )) {
124- gr1 <- function (v ) { do.call(grad_fun , c(list (v ), extra_args_list )) }
133+ gr1 <- function (v ) {
134+ # Catch errors in user function and return -Inf with message as attribute
135+ tryCatch({
136+ do.call(grad_fun , c(list (v ), extra_args_list ))
137+ }, error = function (e ) {
138+ res <- rep(- Inf , length(v ))
139+ attr(res , " message" ) <- e $ message
140+ res
141+ })
142+ }
125143 for (chain in seq_len(num_chains )) {
126144 validate_function(gr1 , inits [[chain ]], extra_args_list , grad = TRUE )
127145 }
0 commit comments