• 94.19% Rate
  • 81 Hits
  • 5 Missed
  • 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
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 1x
  • 1x
  • 1x
  • 50x
  • 50x
  • 50x
  • 50x
  • 41x
  • 9x
  • 42x
  • 33x
  • 24x
  • 9x
  • 9x
  • 1x
  • 54x
  • 18x
  • 36x
  • 33x
  • 33x
  • 27x
  • 198x
  • 33x
  • 132x
  • 50x
  • 1x
  • 143x
  • 143x
  • 143x
  • 143x
  • 41x
  • 4x
  • 4x
  • 39x
  • 30x
  • 1x
  • 29x
  • 30x
  • 9x
  • 18x
  • 18x
  • 102x
  • 82x
  • 30x
  • 52x
  • 20x
  • 13x
  • 7x
  • 102x
  • 102x
  • 143x
  • 143x
  • 11x
  • 132x
  • 100x
  • 132x
  • 100x
  • 143x
  • 111x
  • 111x
  • 143x
  • 143x
  • 143x
  • 4862x
  • 4862x
  • 4719x
  • 4719x
  • 3892x
  • 136x
  • 143x
  • 1x
  • 37x
  • 77x
  • 40x
  • 37x
  • 1x
  • 36x
  • 139x
  • 103x
  • 37x
  • 1x
  • -------------------------------------------------------------------------------
  • -- Generic error messages for the PUMAS wrapper
  • -- Author: Valentin Niess
  • -- License: GNU LGPL-3.0
  • -------------------------------------------------------------------------------
  • local error_ = {}
  • -------------------------------------------------------------------------------
  • -- Table of API functions
  • --
  • -- This table keeps weak references of all pumas API functions. It is used for
  • -- computing the stack depth when raising an error.
  • -------------------------------------------------------------------------------
  • local api_functions = setmetatable({}, {__mode='k'})
  • error_.api_functions = api_functions
  • -------------------------------------------------------------------------------
  • -- Register an API function or metatable
  • -------------------------------------------------------------------------------
  • do
  • local function register_meta (k, v, index)
  • v = v[index]
  • if v then
  • k = k..'.'..index
  • if type(v) == 'function' then
  • api_functions[v] = k
  • elseif type(v) == 'table' then
  • for ki, vi in pairs(v) do
  • if type(vi) == 'function' then
  • api_functions[vi] = k..'.'..ki
  • end
  • end
  • local mt = getmetatable(v)
  • if mt and mt[index] then
  • api_functions[mt[index]] = k
  • end
  • end
  • end
  • end
  • function error_.register (k, v)
  • if type(v) == 'function' then
  • api_functions[v] = k
  • elseif type(v) == 'table' then
  • local mt = getmetatable(v)
  • if mt and mt.__call then
  • api_functions[mt.__call] = k..'.__call'
  • end
  • for _, attribute in ipairs{'__add', '__index', '__newindex',
  • '__sub'} do
  • if rawget(v, attribute) then
  • register_meta(k, v, attribute)
  • end
  • end
  • end
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- Generic error messages formater
  • -------------------------------------------------------------------------------
  • function error_.raise (args)
  • local msg
  • if type(args) == 'string' then
  • msg = args
  • else
  • -- Format the error message
  • msg = {}
  • if args.header ~= nil then
  • table.insert(msg, args.header)
  • elseif args.type ~= nil then
  • if args.bad_member ~= nil then
  • table.insert(msg, "'"..args.type.."' has no member named '"..
  • args.bad_member.."'")
  • elseif args.not_mutable ~= nil then
  • local k
  • if type(args.not_mutable) == 'number' then
  • k = 'entry #'..args.not_mutable
  • else
  • k = "'"..args.not_mutable.."'"
  • end
  • table.insert(msg, "cannot modify "..k.." for '"..args.type.."'")
  • elseif args.argname ~= nil then
  • table.insert(msg, "bad attribute '"..args.argname.."' for '"..
  • args.type.."'")
  • else
  • table.insert(msg, "an unknown error occured related to '"..
  • args.type.."'")
  • end
  • else
  • if args.argnum ~= nil then
  • if args.argnum == 'bad' then
  • table.insert(msg, 'bad number of argument(s)')
  • else
  • table.insert(msg, 'bad argument #'..args.argnum)
  • end
  • elseif args.argname ~= nil then
  • table.insert(msg, "bad argument '"..args.argname.."'")
  • else
  • table.insert(msg, "bad argument(s)")
  • end
  • if args.fname ~= nil then
  • table.insert(msg, "to '"..args.fname.."'")
  • end
  • end
  • local description = {}
  • if args.description ~= nil then
  • table.insert(description, args.description)
  • else
  • if args.expected ~= nil then
  • table.insert(description, 'expected '..args.expected)
  • end
  • if args.got ~= nil then
  • table.insert(description, 'got '..args.got)
  • end
  • end
  • if #description > 0 then
  • description = table.concat(description, ', ')
  • table.insert(msg, '('..description..')')
  • end
  • msg = table.concat(msg, ' ')
  • end
  • -- Compute the depth
  • local depth = 1
  • do
  • local level = 1
  • while true do
  • local info = debug.getinfo(level, 'Sf')
  • if not info or (info.what == 'main') then break end
  • level = level + 1
  • if info.what == 'Lua' then
  • if api_functions[info.func] then
  • depth = level
  • end
  • end
  • end
  • end
  • error(msg, depth)
  • end
  • -------------------------------------------------------------------------------
  • -- Generate an error function with default format parameters
  • -------------------------------------------------------------------------------
  • function error_.ErrorFunction (default_args)
  • return function (extra_args)
  • local args = {}
  • for k, v in pairs(default_args) do
  • args[k] = v
  • end
  • if type(extra_args) == 'string' then
  • args.description = extra_args
  • elseif extra_args ~= nil then
  • for k, v in pairs(extra_args) do
  • args[k] = v
  • end
  • end
  • error_.raise(args)
  • end
  • end
  • -------------------------------------------------------------------------------
  • -- Return the package
  • -------------------------------------------------------------------------------
  • return error_