Skip to content

Commit

Permalink
fix: Introduce localized strings for new errors (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiber authored Nov 15, 2024
1 parent 85270ac commit 94366a5
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 353 deletions.
15 changes: 15 additions & 0 deletions src/DynamicExpresso.Core/DynamicExpresso.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\ErrorMessages.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>ErrorMessages.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\ErrorMessages.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ErrorMessages.resx</DependentUpon>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Runtime.Serialization;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Exceptions
{
[Serializable]
public class AssignmentOperatorDisabledException : ParseException
{
public AssignmentOperatorDisabledException(string operatorString, int position)
: base(string.Format("Assignment operator '{0}' not allowed", operatorString), position)
: base(string.Format(ErrorMessages.AssignmentOperatorNotAllowed, operatorString), position)
{
OperatorString = operatorString;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Runtime.Serialization;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Exceptions
{
[Serializable]
public class DuplicateParameterException : DynamicExpressoException
{
public DuplicateParameterException(string identifier)
: base(string.Format("The parameter '{0}' was defined more than once", identifier))
: base(string.Format(ErrorMessages.DuplicateParameter, identifier))
{
Identifier = identifier;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Runtime.Serialization;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Exceptions
{
[Serializable]
public class NoApplicableMethodException : ParseException
{
public NoApplicableMethodException(string methodName, string methodTypeName, int position)
: base(string.Format("No applicable method '{0}' exists in type '{1}'", methodName, methodTypeName), position)
: base(string.Format(ErrorMessages.InvalidMethodCall2, methodName, methodTypeName), position)
{
MethodTypeName = methodTypeName;
MethodName = methodName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Runtime.Serialization;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Exceptions
{
[Serializable]
public class ReflectionNotAllowedException : ParseException
{
public ReflectionNotAllowedException()
: base("Reflection expression not allowed. To enable reflection use Interpreter.EnableReflection().", 0)
: base(ErrorMessages.ReflectionNotAllowed, 0)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Runtime.Serialization;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Exceptions
{
[Serializable]
public class UnknownIdentifierException : ParseException
{
public UnknownIdentifierException(string identifier, int position)
: base(string.Format("Unknown identifier '{0}'", identifier), position)
: base(string.Format(ErrorMessages.UnknownIdentifier, identifier), position)
{
Identifier = identifier;
}
Expand Down
5 changes: 3 additions & 2 deletions src/DynamicExpresso.Core/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Linq.Expressions;
using DynamicExpresso.Exceptions;
using DynamicExpresso.Resources;

namespace DynamicExpresso
{
Expand Down Expand Up @@ -134,7 +135,7 @@ public Interpreter SetDefaultNumberType(DefaultNumberType defaultNumberType)
}

/// <summary>
/// Allows to enable/disable assignment operators.
/// Allows to enable/disable assignment operators.
/// For security when expression are generated by the users is more safe to disable assignment operators.
/// </summary>
/// <param name="assignmentOperators"></param>
Expand Down Expand Up @@ -278,7 +279,7 @@ public Interpreter SetIdentifier(Identifier identifier)
throw new ArgumentNullException(nameof(identifier));

if (LanguageConstants.ReservedKeywords.Contains(identifier.Name))
throw new InvalidOperationException($"{identifier.Name} is a reserved word");
throw new InvalidOperationException(string.Format(ErrorMessages.ReservedWord, identifier.Name));

_settings.Identifiers[identifier.Name] = identifier;

Expand Down
3 changes: 2 additions & 1 deletion src/DynamicExpresso.Core/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Runtime.ExceptionServices;
using DynamicExpresso.Reflection;
using DynamicExpresso.Resources;

namespace DynamicExpresso
{
Expand Down Expand Up @@ -87,7 +88,7 @@ public object Invoke(params object[] args)
if (args != null)
{
if (declaredParameters.Length != args.Length)
throw new InvalidOperationException("Arguments count mismatch.");
throw new InvalidOperationException(ErrorMessages.ArgumentCountMismatch);

for (var i = 0; i < args.Length; i++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/DynamicExpresso.Core/Parsing/InterpreterExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using DynamicExpresso.Exceptions;
using DynamicExpresso.Reflection;
using DynamicExpresso.Resources;

namespace DynamicExpresso.Parsing
{
Expand Down Expand Up @@ -35,7 +36,7 @@ public InterpreterExpression(ParserArguments parserArguments, string expressionT
{
if (settings.Identifiers.ContainsKey(myParameter.Name))
{
throw new ParseException($"A local or parameter named '{myParameter.Name}' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter", myParameter.Position);
throw new ParseException(string.Format(ErrorMessages.DuplicateLocalParameterDeclaration, myParameter.Name), myParameter.Position);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private ParameterWithPosition[] ParseLambdaParameterList()
}

if (!hasOpenParen && parameters.Length > 1)
throw new ParseException("Multiple lambda parameters detected, but with no surrounding parenthesis", _parsePosition);
throw new ParseException(ErrorMessages.MultipleLambdaParametersWithoutBrace, _parsePosition);

return parameters;
}
Expand Down Expand Up @@ -508,7 +508,7 @@ public bool IsShiftOperator(out ExpressionType shiftType)
// << could be a token, but is not for symmetry
else if (_token.id == TokenId.LessThan && _parseChar == '<')
{
NextToken(); // consume next <
NextToken(); // consume next <
shiftType = ExpressionType.LeftShift;
return true;
}
Expand Down Expand Up @@ -1397,7 +1397,7 @@ private Type ParseKnownType()

private bool TryParseKnownType(string name, out Type type)
{
// if the type is unknown, we need to restart parsing
// if the type is unknown, we need to restart parsing
var originalPos = _token.pos;

// the name might reference a generic type, with an aliased name (e.g. List<T> = MyList instead of List`1)
Expand Down Expand Up @@ -1433,7 +1433,7 @@ private Type ParseKnownGenericType(string name, Type type)
return null;

if (rank != type.GetGenericArguments().Length)
throw new ArgumentException($"The number of generic arguments provided doesn't equal the arity of the generic type definition.");
throw new ArgumentException(ErrorMessages.GenericArgumentCountMismatch);

// there are actual type arguments: instantiate the proper generic type
if (typeArguments.All(_ => _ != null))
Expand Down Expand Up @@ -1959,7 +1959,7 @@ private Expression GenerateGreaterThan(Expression left, Expression right)
return GenerateBinary(ExpressionType.GreaterThan, left, right);
}



private Expression GenerateGreaterThanEqual(Expression left, Expression right)
{
Expand Down Expand Up @@ -2533,7 +2533,7 @@ private void NextToken()
if (_parsePosition == _expressionTextLength)
{
if (_token.id == TokenId.End)
throw new InvalidOperationException("NextToken called when already at the end of the expression");
throw new InvalidOperationException(ErrorMessages.NextTokenAtEnd);

t = TokenId.End;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/DynamicExpresso.Core/ReferenceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using DynamicExpresso.Reflection;
using DynamicExpresso.Resources;

namespace DynamicExpresso
{
Expand Down Expand Up @@ -30,7 +31,7 @@ public ReferenceType(string name, Type type)
var genericType = type.GetGenericTypeDefinition();
var genericTypeName = genericType.Name.Substring(0, genericType.Name.IndexOf('`'));
genericTypeName += $"<{new string(',', genericType.GetGenericArguments().Length - 1)}>";
throw new ArgumentException($"Generic type must be referenced via its generic definition: {genericTypeName}");
throw new ArgumentException(string.Format(ErrorMessages.GenericTypeReference, genericTypeName));
}

Type = type;
Expand Down
Loading

0 comments on commit 94366a5

Please sign in to comment.