From 3983e0fc4c1b7aff93ec5e1d6855769b4b89b78a Mon Sep 17 00:00:00 2001 From: Tamas Gal <himself@tamasgal.com> Date: Thu, 5 Dec 2024 11:39:59 +0100 Subject: [PATCH] Improve error reporting on getindex of Detector --- src/hardware.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/hardware.jl b/src/hardware.jl index a4886d1f..6b170248 100644 --- a/src/hardware.jl +++ b/src/hardware.jl @@ -298,8 +298,16 @@ function Base.iterate(d::Detector, state=(Int[], 1)) end (d.modules[module_ids[count]], (module_ids, count + 1)) end -Base.getindex(d::Detector, module_id::Integer) = d.modules[module_id] -Base.getindex(d::Detector, string::Integer, floor::Integer) = d.locations[string, floor] +function Base.getindex(d::Detector, module_id::Integer) + haskey(d.modules, module_id) && return d.modules[module_id] + error("Module with ID $(module_id) not found.") +end +function Base.getindex(d::Detector, string::Integer, floor::Integer) + haskey(d.locations, (string, floor)) && return d.locations[string, floor] + available_strings = join(d.strings, ", ", " and ") + !(hasstring(d, string)) && error("String $(string) not found. Available strings: $(available_strings).") + error("String $(string) has no module at floor $(floor).") +end """ Return the detector module for a given module ID. """ -- GitLab