Better error handling

This commit is contained in:
Deukhoofd 2019-01-22 12:28:21 +01:00
parent cd6384614b
commit 284ba2cf54
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
8 changed files with 43 additions and 25 deletions

View File

@ -107,7 +107,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
// if we haven't found a method, we throw an exception
if (method == null)
{
throw new ScriptRuntimeException(
throw new ScriptRuntimeException(script.FileName,
$"Can't find method {Method.Name} with parameter types {string.Join(", ", convertedTypes.Select(x => x.Name))} on type {_object.GetType().Name}",
span, diagnostics.ScriptString.GetSpan(span));
}

View File

@ -6,6 +6,7 @@ using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols;
using Upsilon.Evaluator;
using Upsilon.Exceptions;
using Upsilon.Text;
namespace Upsilon.BaseTypes.ScriptFunction
@ -25,7 +26,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
var option = GetValidOption(variables);
if (option == null)
{
throw new EvaluationException(
throw new EvaluationException(script.FileName,
$"No valid function found");
}
return option.Run(diagnostics, variables, script, scope, span);

View File

@ -64,7 +64,7 @@ namespace Upsilon.BaseTypes.UserData
}
else
{
throw new ScriptRuntimeException(
throw new ScriptRuntimeException(null,
$"Tried indexing a CSharp list with a value that's not an integer. Value: {scriptIndex.ToCSharpObject()}",
span, diagnostics.ScriptString.GetSpan(span));
}

View File

@ -375,6 +375,8 @@ namespace Upsilon.Binder
{
return new UserDataVariableSymbol(fullStopIndexExpression.Index, boundDef, true, parent);
}
diagnostics?.LogWarning($"Can't resolve type '{bDefProperty.ToString()}'", fullStopIndexExpression.Span);
return new VariableSymbol(fullStopIndexExpression.Index, Type.Unknown, true);
}
}
else if (indexerVariable.TypeContainer == Type.Unknown)
@ -887,7 +889,8 @@ namespace Upsilon.Binder
}
else
{
var functionParameter = (UserDataVariableSymbol) variableSymbol;
if (variableSymbol is UserDataVariableSymbol functionParameter)
{
var udBoundDef = (UserDataBoundTypeDefinition)functionParameter.BoundTypeDefinition;
if (udBoundDef.Properties.TryGetValue(index.ToLowerInvariant(), out var property))
{
@ -896,6 +899,7 @@ namespace Upsilon.Binder
_diagnostics.LogError($"No variable '{index}' found on type '{udBoundDef.Name}'.",
e.Span);
}
}
return new BoundFullStopIndexExpression(expression, index, Type.Unknown, e.Span);
case Type.Unknown:
return new BoundFullStopIndexExpression(expression, index, Type.Unknown, e.Span);

View File

@ -1,12 +0,0 @@
using System;
namespace Upsilon.Evaluator
{
public class EvaluationException : Exception
{
public EvaluationException(string message) : base(message)
{
}
}
}

View File

@ -88,7 +88,7 @@ namespace Upsilon.Evaluator
var function = (ScriptRuntimeFunction) statement;
var option = function.GetValidOption(parameters);
if (option == null)
throw new EvaluationException(
throw new EvaluationException(_script.FileName,
$"No function found with name '{functionName}' and available parameters {string.Join(", ", parameters.Select(x => $"{x.GetType().Name}"))}");
var result = option.Run(_diagnostics, parameters?.Select(x => x.ToScriptType()).ToArray(), _script, Scope, new TextSpan());
@ -399,7 +399,7 @@ namespace Upsilon.Evaluator
private void ThrowException(string message, TextSpan location)
{
throw new ScriptRuntimeException(message, location, _diagnostics.ScriptString.GetSpan(location));
throw new ScriptRuntimeException(_script.FileName, message, location, _diagnostics.ScriptString.GetSpan(location));
}
private void EvaluateAssignmentStatement(BoundVariableAssignment e)

View File

@ -0,0 +1,23 @@
using System;
namespace Upsilon.Exceptions
{
public class EvaluationException : Exception
{
public string FileName { get; }
public string ErrorMessage { get; }
public EvaluationException(string fileName, string message)
{
FileName = fileName;
ErrorMessage = message;
}
public override string ToString()
{
return $"[{FileName}] {ErrorMessage}";
}
public override string Message => ToString();
}
}

View File

@ -5,13 +5,15 @@ namespace Upsilon.Exceptions
{
public class ScriptRuntimeException : Exception
{
public string FileName { get; }
public string ErrorMessage { get; }
public int Line { get; }
public int Character { get; }
public string ErrorLine { get; }
public ScriptRuntimeException(string errorMessage, TextSpan position, string errorLine)
public ScriptRuntimeException(string fileName, string errorMessage, TextSpan position, string errorLine)
{
FileName = fileName;
ErrorMessage = errorMessage;
Line = position.StartLine;
Character = position.StartPosition;
@ -20,7 +22,7 @@ namespace Upsilon.Exceptions
public override string ToString()
{
return $"{ErrorMessage} at ({Line}, {Character})\n{ErrorLine}";
return $"[{FileName}] {ErrorMessage} at ({Line}, {Character})\n{ErrorLine}";
}
public override string Message => ToString();