Error Handling & Logging
BidsErrorHandler and BidsLogger are the two utility classes that centralise
error propagation and log formatting throughout the plugin.
BidsErrorHandler
All public methods are static, @CompileStatic, and slf4j-backed.
tryWithContext
static <T> T tryWithContext(String context, Closure<T> closure)
Wraps any closure with a named context. On success, returns the closure result.
On exception, logs [context] Error occurred: <message> at ERROR level, emits the
stack trace at DEBUG level, and re-throws as RuntimeException("Error in context: …").
Use this for critical operations that must not swallow failures:
BidsErrorHandler.tryWithContext("nf-bids-parser") {
dataset.loadParticipants()
}
safeExecute
static <T> T safeExecute(String context, Closure<T> closure, T defaultValue = null)
Like tryWithContext but catches exceptions, logs at WARN level, and returns
defaultValue instead of re-throwing. Use for optional operations where a fallback
is acceptable.
validateWithContext
static void validateWithContext(boolean condition, String context, String message)
Asserts condition; throws IllegalStateException("[context] message") if false.
handleError / handleErrorWithMessage
static void handleError(String context, Exception e)
static void handleErrorWithMessage(String context, String message)
Both log at ERROR level and throw RuntimeException. Use when you have already
caught an exception and want to re-package it with context.
createDetailedError
static String createDetailedError(String error, List<String> suggestions)
Formats a multi-line error string with a numbered suggestions list. Used by
LibBidsShWrapper for the "libBIDS.sh not found" diagnostic.
Custom exception types
Three domain-specific exception classes are nested inside BidsErrorHandler:
| Class | When to use |
|---|---|
|
Runtime processing failures (file I/O, parsing). |
|
BIDS structure or config validation failures. |
|
Configuration loading or structural errors. |
All extend RuntimeException and accept both (String message) and
(String message, Throwable cause) constructors.
BidsLogger
All methods are static and delegate to an slf4j logger named for the class.
Callers may pass an explicit context string (injected as [context] prefix) or
use the single-argument overloads which default to the "nf-bids" context.
Log methods
| Method | Level / format |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|