-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherrorHandling.m
64 lines (54 loc) · 1.56 KB
/
errorHandling.m
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
function errorHandling(varargin)
%
% USAGE::
%
% errorHandling(functionName, id, msg, tolerant, verbose)
%
% :param functionName:
% :type functionName: char
%
% :param id: Error or warning id
% :type id: char
%
% :param msg: Message to print
% :type msg: char
%
% :param tolerant: If set to ``true`` errors are converted into warnings
% :type tolerant: logical
%
% :param verbose: If set to ``0`` or ``false`` this will silence any warning
% :type verbose: logical
%
% EXAMPLE::
%
% msg = sprintf('this error happened with this file %s', filename)
% id = 'thisError';
% errorHandling(mfilename(), id, msg, true, opt.verbosity)
%
%
% adapted from bids-matlab
%
% (C) Copyright 2018 bidspm developers
defaultFunctionName = 'bidspm';
defaultId = 'unspecified';
defaultMsg = 'unspecified';
defaultTolerant = true;
defaultVerbose = false;
args = inputParser;
addOptional(args, 'functionName', defaultFunctionName, @ischar);
addOptional(args, 'id', defaultId, @ischar);
addOptional(args, 'msg', defaultMsg, @ischar);
addOptional(args, 'tolerant', defaultTolerant, @islogical);
addOptional(args, 'verbose', defaultVerbose, @(x) (islogical(x) || isnumeric(x)));
parse(args, varargin{:});
functionName = spm_file(args.Results.functionName, 'basename');
id = [functionName, ':' args.Results.id];
if ~args.Results.tolerant
errorStruct.identifier = id;
errorStruct.message = args.Results.msg;
error(errorStruct);
end
if args.Results.verbose > 0
warning(id, args.Results.msg);
end
end