From 7392ccf3d8e94975b0d9c1961d93f05daa4a7e5e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 26 Jun 2024 17:04:28 -0700 Subject: [PATCH] typing.lua: report errors from the caller's location Don't attribute the error to the metamethods in the typing module, attribute them to the calling code. --- assets/policy-extras/typing.lua | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/assets/policy-extras/typing.lua b/assets/policy-extras/typing.lua index d186f666..8ea7dcb1 100644 --- a/assets/policy-extras/typing.lua +++ b/assets/policy-extras/typing.lua @@ -26,7 +26,8 @@ function mod.record(name, fields) local field_def = ty.fields[k] if not field_def then error( - string.format("%s: attempt to read unknown field '%s'", ty.name, k) + string.format("%s: attempt to read unknown field '%s'", ty.name, k), + 2 ) end end @@ -34,7 +35,7 @@ function mod.record(name, fields) function ty.__newindex(t, k, v) local field_def = ty.fields[k] if not field_def then - error(string.format("%s: unknown field '%s'", ty.name, k)) + error(string.format("%s: unknown field '%s'", ty.name, k), 2) end local status, result = field_def:validate_value(v) if status then @@ -47,7 +48,8 @@ function mod.record(name, fields) value_dump(v), k, result - ) + ), + 2 ) end end @@ -68,7 +70,8 @@ function mod.record(name, fields) ty.name, k, def.name - ) + ), + 2 ) end end @@ -153,7 +156,8 @@ function mod.list(value_type) function ty.__newindex(t, idx, v) if type(idx) ~= 'number' then error( - string.format("%s: invalid index '%s' list", ty.name, value_dump(idx)) + string.format("%s: invalid index '%s' list", ty.name, value_dump(idx)), + 2 ) end @@ -166,7 +170,8 @@ function mod.list(value_type) value_dump(v), idx, val_fixup - ) + ), + 2 ) end @@ -257,7 +262,8 @@ function mod.map(key_type, value_type) ty.name, value_dump(k), key_fixup - ) + ), + 2 ) end @@ -270,7 +276,8 @@ function mod.map(key_type, value_type) value_dump(v), value_dump(k), val_fixup - ) + ), + 2 ) end @@ -340,7 +347,7 @@ local function make_simple_ctor(ty) function ctor_mt:__call(value) local status, fixup = ty:validate_value(value) if not status then - error(err) + error(err, 2) end return fixup end