From d382506bc568e6e20a10dca324371b46899f9772 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Sun, 30 Jul 2023 20:11:00 -0400 Subject: [PATCH] improve wrong dir message --- +bids/layout.m | 22 +++++++++++++++++++++- tests/tests_layout/test_layout.m | 6 ++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/+bids/layout.m b/+bids/layout.m index 04a7d34e..f3cb91e4 100644 --- a/+bids/layout.m +++ b/+bids/layout.m @@ -83,7 +83,11 @@ addParameter(args, 'use_schema', default_use_schema); addParameter(args, 'verbose', default_verbose); - parse(args, varargin{:}); + try + parse(args, varargin{:}); + catch ME + handle_invalid_input(ME, varargin{1}); + end root = args.Results.root; index_derivatives = args.Results.index_derivatives; @@ -212,6 +216,22 @@ end +function handle_invalid_input(ME, root) + % TODO improve as this may send the wrong message on octave + % for ANY failed input parsing + if (~bids.internal.is_octave && ... + bids.internal.starts_with(ME.message, ... + 'The value of ''root''')) || ... + bids.internal.is_octave + if ischar(root) + msg = sprintf(['First input argument must be an existing directory.'... + '\nGot: ''%s.'''], root); + bids.internal.error_handling(mfilename(), 'InvalidInput', ... + msg, false); + end + end +end + function value = exclude(filter, entity, label) value = false; % skip if not included in filter diff --git a/tests/tests_layout/test_layout.m b/tests/tests_layout/test_layout.m index ec9070f1..3f2e6425 100644 --- a/tests/tests_layout/test_layout.m +++ b/tests/tests_layout/test_layout.m @@ -6,6 +6,12 @@ initTestSuite; end +function test_layout_error_message + + assertExceptionThrown(@()bids.layout('foo'), 'layout:InvalidInput'); + +end + function test_layout_filter() verbose = false;