From 1d24be85d6f38bcc0f26fce31dd3a166e46af864 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 21 Nov 2018 14:11:52 +0100 Subject: [PATCH] Better error handling of getting userdata members --- Upsilon/BaseTypes/UserData/UserData.cs | 4 ++-- Upsilon/BaseTypes/UserData/UserDataType.cs | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Upsilon/BaseTypes/UserData/UserData.cs b/Upsilon/BaseTypes/UserData/UserData.cs index 4565c14..592fa85 100644 --- a/Upsilon/BaseTypes/UserData/UserData.cs +++ b/Upsilon/BaseTypes/UserData/UserData.cs @@ -27,10 +27,10 @@ namespace Upsilon.BaseTypes.UserData public LuaType Get(Diagnostics diagnostics, TextSpan span, string s, EvaluationScope scope) { - var (type, failed) = _typeInfo.Get(Value, s); + var (type, failed, error) = _typeInfo.Get(Value, s); if (failed) { - diagnostics.LogError($"Cannot find member '{s}' on type '{Value.GetType()}'", span); + diagnostics.LogError(error, span); } return type; } diff --git a/Upsilon/BaseTypes/UserData/UserDataType.cs b/Upsilon/BaseTypes/UserData/UserDataType.cs index b51004c..01f0ef3 100644 --- a/Upsilon/BaseTypes/UserData/UserDataType.cs +++ b/Upsilon/BaseTypes/UserData/UserDataType.cs @@ -33,27 +33,28 @@ namespace Upsilon.BaseTypes.UserData private Dictionary Methods { get; } private UserDataTypeOperators OperatorHandler { get; } - public (LuaType Type, bool Failed) Get(object value, string member) + public (LuaType Type, bool Failed, string Error) Get(object value, string member) { if (value.GetType() != Type) - return (null, true); + return (null, true, "Invalid Type"); member = member.ToLowerInvariant(); if (Variables.TryGetValue(member, out var info)) { - return (info.GetValue(value).ToLuaType(), false); + return (info.GetValue(value).ToLuaType(), false, null); } if (Properties.TryGetValue(member, out var property)) { - return (property.GetValue(value).ToLuaType(), false); + return (property.GetValue(value).ToLuaType(), false, null); } if (Methods.TryGetValue(member, out var method)) { - return (new LuaMethodInfoFunction(method, value), false); + return (new LuaMethodInfoFunction(method, value), false, null); } - return (null, true); + + return (null, true, $"Can't find public member '{member}' on type '{Type}'."); } - public (bool failed, string error) Set(object value, string member, LuaType newValue) + public (bool Failed, string Error) Set(object value, string member, LuaType newValue) { member = member.ToLowerInvariant(); if (value.GetType() != Type)