Skip to content

Commit

Permalink
Mono: Replace exception strings with those stored in the resx file (#…
Browse files Browse the repository at this point in the history
…78341)

Fixes #34056

Co-authored-by: Dan Moseley <danmose@microsoft.com>
Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
  • Loading branch information
3 people authored Mar 23, 2023
1 parent 0b3f4dc commit 830d3d0
Show file tree
Hide file tree
Showing 27 changed files with 310 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1189,12 +1189,12 @@ public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccess

if (!excType.IsSubclassOf(typeof(Exception)) && excType != typeof(Exception))
{
throw new ArgumentException(SR.Argument_NotExceptionType);
throw new ArgumentException(SR.Argument_NotExceptionType, nameof(excType));
}
ConstructorInfo? con = excType.GetConstructor(Type.EmptyTypes);
if (con == null)
{
throw new ArgumentException(SR.Argument_MissingDefaultConstructor);
throw new ArgumentException(SR.Arg_NoDefCTorWithoutTypeName, nameof(excType));
}
Emit(OpCodes.Newobj, con);
Emit(OpCodes.Throw);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/debug/ee/funceval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ void ResolveFuncEvalGenericArgInfo(DebuggerEval *pDE)
// If this is a new object operation, then we should have a .ctor.
if ((pDE->m_evalType == DB_IPCE_FET_NEW_OBJECT) && !pDE->m_md->IsCtor())
{
COMPlusThrow(kArgumentException, W("Argument_MissingDefaultConstructor"));
COMPlusThrow(kArgumentException, W("Arg_NoDefCTorWithoutTypeName"));
}

pDE->m_md->EnsureActive();
Expand Down
185 changes: 181 additions & 4 deletions src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/mono/System.Private.CoreLib/src/Mono/HotReload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static FieldStore Create (RuntimeTypeHandle type)
else if (t.IsClass || t.IsInterface)
loc = null;
else
throw new ArgumentException("EnC: Expected a primitive, valuetype, class or interface field");
throw new ArgumentException(SR.Arg_EnC_ExpectedPrimitive);
/* FIXME: do we want FieldStore to be pinned? */
return new FieldStore(loc);
}
Expand Down
4 changes: 2 additions & 2 deletions src/mono/System.Private.CoreLib/src/System/Array.Mono.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ private static void Copy(Array sourceArray, int sourceIndex, Array destinationAr
throw new RankException(SR.Rank_MultiDimNotSupported);

if (sourceIndex < 0)
throw new ArgumentOutOfRangeException(nameof(sourceIndex), "Value has to be >= 0.");
throw new ArgumentOutOfRangeException(nameof(sourceIndex), SR.ArgumentOutOfRange_NeedNonNegNum);

if (destinationIndex < 0)
throw new ArgumentOutOfRangeException(nameof(destinationIndex), "Value has to be >= 0.");
throw new ArgumentOutOfRangeException(nameof(destinationIndex), SR.ArgumentOutOfRange_NeedNonNegNum);

var src = sourceArray;
var dst = destinationArray;
Expand Down
14 changes: 7 additions & 7 deletions src/mono/System.Private.CoreLib/src/System/ModuleHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public int MDStreamVersion
get
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
return RuntimeModule.GetMDStreamVersion(value);
}
}
Expand Down Expand Up @@ -68,10 +68,10 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken)
public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
IntPtr res = RuntimeModule.ResolveTypeToken(value, typeToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new TypeLoadException(string.Format("Could not load type '0x{0:x}' from assembly '0x{1:x}'", typeToken, value.ToInt64()));
throw new TypeLoadException(SR.Format(SR.ClassLoad_General_Hex, typeToken, value.ToInt64()));
else
return new RuntimeTypeHandle(res);
}
Expand All @@ -80,10 +80,10 @@ public RuntimeTypeHandle ResolveTypeHandle(int typeToken, RuntimeTypeHandle[]? t
public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);
IntPtr res = RuntimeModule.ResolveMethodToken(value, methodToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new Exception(string.Format("Could not load method '0x{0:x}' from assembly '0x{1:x}'", methodToken, value.ToInt64()));
throw new Exception(SR.Format(SR.ClassLoad_General_Hex, methodToken, value.ToInt64()));
else
return new RuntimeMethodHandle(res);
}
Expand All @@ -92,11 +92,11 @@ public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandl
public RuntimeFieldHandle ResolveFieldHandle(int fieldToken, RuntimeTypeHandle[]? typeInstantiationContext, RuntimeTypeHandle[]? methodInstantiationContext)
{
if (value == IntPtr.Zero)
throw new ArgumentNullException(string.Empty, "Invalid handle");
throw new ArgumentNullException(string.Empty, SR.Arg_InvalidHandle);

IntPtr res = RuntimeModule.ResolveFieldToken(value, fieldToken, ptrs_from_handles(typeInstantiationContext), ptrs_from_handles(methodInstantiationContext), out _);
if (res == IntPtr.Zero)
throw new Exception(string.Format("Could not load field '0x{0:x}' from assembly '0x{1:x}'", fieldToken, value.ToInt64()));
throw new Exception(SR.Format(SR.ClassLoad_General_Hex, fieldToken, value.ToInt64()));
else
return new RuntimeFieldHandle(res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
{
Type t = fi.DeclaringType!;
if ((atype != t) && (!t.IsSubclassOf(atype)) && (!atype.IsSubclassOf(t)))
throw new ArgumentException("Field '" + fi.Name + "' does not belong to the same class as the constructor");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotBelongToConstructorClass, fi.Name));
if (!IsValidType(fi.FieldType))
throw new ArgumentException("Field '" + fi.Name + "' does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotHaveAValidType, fi.Name));
if (!IsValidValue(fi.FieldType, fieldValues[i]))
throw new ArgumentException("Field " + fi.Name + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_FieldDoesNotHaveAValidValue, fi.Name));
// FIXME: Check enums and TypeBuilders as well
if (fieldValues[i] != null)
// IsEnum does not seem to work on TypeBuilders
Expand All @@ -215,7 +215,7 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
// MS.NET allows this
//
if (!fi.FieldType.IsArray)
throw new ArgumentException("Value of field '" + fi.Name + "' does not match field type: " + fi.FieldType);
throw new ArgumentException(SR.Format(SR.Argument_UnmatchedFieldValueAndType, fi.Name, fi.FieldType));
}
i++;
}
Expand All @@ -224,19 +224,19 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
foreach (PropertyInfo pi in namedProperties)
{
if (!pi.CanWrite)
throw new ArgumentException("Property '" + pi.Name + "' does not have a setter.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyMissingSetter, pi.Name));
Type t = pi.DeclaringType!;
if ((atype != t) && (!t.IsSubclassOf(atype)) && (!atype.IsSubclassOf(t)))
throw new ArgumentException("Property '" + pi.Name + "' does not belong to the same class as the constructor");
throw new ArgumentException(SR.Format(SR.Argument_PropertyClassUnmatchedWithConstructor, pi.Name));
if (!IsValidType(pi.PropertyType))
throw new ArgumentException("Property '" + pi.Name + "' does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyInvalidType, pi.Name));
if (!IsValidValue(pi.PropertyType, propertyValues[i]))
throw new ArgumentException("Property " + pi.Name + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_PropertyInvalidValue, pi.Name));
if (propertyValues[i] != null)
{
if (!(pi.PropertyType is TypeBuilder) && !pi.PropertyType.IsEnum && !pi.PropertyType.IsInstanceOfType(propertyValues[i]))
if (!pi.PropertyType.IsArray)
throw new ArgumentException("Value of property '" + pi.Name + "' does not match property type: " + pi.PropertyType + " -> " + propertyValues[i]);
throw new ArgumentException(SR.Format(SR.Argument_PropertyUnmatchingPropertyType, pi.Name, pi.PropertyType, propertyValues[i]));
}
i++;
}
Expand All @@ -248,17 +248,17 @@ private void Initialize(ConstructorInfo con, object?[] constructorArgs,
{
Type paramType = pi.ParameterType;
if (!IsValidType(paramType))
throw new ArgumentException("Parameter " + i + " does not have a valid type.");
throw new ArgumentException(SR.Format(SR.Argument_ParameterInvalidType, i));
if (!IsValidValue(paramType, constructorArgs[i]))
throw new ArgumentException("Parameter " + i + " is not a valid value.");
throw new ArgumentException(SR.Format(SR.Argument_ParameterInvalidValue, i));

if (constructorArgs[i] != null)
{
if (!(paramType is TypeBuilder) && !paramType.IsEnum && !paramType.IsInstanceOfType(constructorArgs[i]))
if (!paramType.IsArray)
throw new ArgumentException("Value of argument " + i + " does not match parameter type: " + paramType + " -> " + constructorArgs[i]);
throw new ArgumentException(SR.Format(SR.Argument_ParameterHasUnmatchedArgumentValue, i, paramType, constructorArgs[i]));
if (!IsValidParam(constructorArgs[i]!, paramType))
throw new ArgumentException("Cannot emit a CustomAttribute with argument of type " + constructorArgs[i]!.GetType() + ".");
throw new ArgumentException(SR.Format(SR.Argument_BadParameterTypeForCAB, constructorArgs[i]!.GetType()));
}
}
i++;
Expand Down Expand Up @@ -403,7 +403,7 @@ internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuild
marshalCookie = decode_string(data, pos, out pos)!;
break;
default:
throw new Exception("Unknown MarshalAsAttribute field: " + named_name);
throw new Exception(SR.Format(SR.Exception_UnknownMarshalAsAttributeField, named_name));
}
}

Expand All @@ -420,7 +420,7 @@ internal static UnmanagedMarshal get_umarshal(CustomAttributeBuilder customBuild
#endif
case UnmanagedType.ByValArray:
if (!is_field)
throw new ArgumentException("Specified unmanaged type is only valid on fields");
throw new ArgumentException(SR.Argument_UnmanagedTypeOnlyValidOnFields);

return UnmanagedMarshal.DefineByValArray(sizeConst);
case UnmanagedType.ByValTStr:
Expand Down Expand Up @@ -451,7 +451,7 @@ private static Type elementTypeToType(int elementType) =>
0x0c => typeof(float),
0x0d => typeof(double),
0x0e => typeof(string),
_ => throw new Exception("Unknown element type '" + elementType + "'"),
_ => throw new Exception(SR.Format(SR.ArgumentException_InvalidTypeArgument, elementType)),
};

private static object? decode_cattr_value(Type t, byte[] data, int pos, out int rpos)
Expand Down Expand Up @@ -480,7 +480,7 @@ private static Type elementTypeToType(int elementType) =>
if (subtype >= 0x02 && subtype <= 0x0e)
return decode_cattr_value(elementTypeToType(subtype), data, pos, out rpos);
else
throw new Exception("Subtype '" + subtype + "' of type object not yet handled in decode_cattr_value");
throw new Exception(SR.Exception_UnhandledSubType);
default:
throw new Exception("FIXME: Type " + t + " not yet handled in decode_cattr_value.");
}
Expand Down Expand Up @@ -508,9 +508,9 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu

// Prolog
if (data.Length < 2)
throw new Exception("Custom attr length is only '" + data.Length + "'");
throw new Exception(SR.Format(SR.Exception_InvalidCustomAttributeLength, data.Length));
if ((data[0] != 0x1) || (data[1] != 0x00))
throw new Exception("Prolog invalid");
throw new Exception(SR.Exception_InvalidProlog);
pos = 2;

ParameterInfo[] pi = GetParameters(ctor);
Expand Down Expand Up @@ -547,7 +547,7 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu
/* Field */
FieldInfo? fi = ctor.DeclaringType!.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (fi == null)
throw new Exception("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'");
throw new Exception(SR.Format(SR.Exception_EmptyFieldForCustomAttributeType, ctor.DeclaringType, name));

object? val = decode_cattr_value(fi.FieldType, data, pos, out pos);
if (enum_type_name != null)
Expand All @@ -560,7 +560,7 @@ internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBu
}
else
// FIXME:
throw new Exception("Unknown named type: " + named_type);
throw new Exception(SR.Format(SR.Exception_UnknownNamedType, named_type));
}

return info;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public ILGenerator GetILGenerator(int streamSize) =>
}
catch (MethodAccessException mae)
{
throw new TargetInvocationException("Method cannot be invoked.", mae);
throw new TargetInvocationException(SR.TargetInvocation_MethodCannotBeInvoked, mae);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public virtual void BeginCatchBlock(Type? exceptionType)
if (!InExceptionBlock)
throw new NotSupportedException(SR.Argument_NotInExceptionBlock);
if (exceptionType != null && exceptionType.IsUserType)
throw new NotSupportedException("User defined subclasses of System.Type are not yet supported.");
throw new NotSupportedException(SR.PlatformNotSupported_UserDefinedSubclassesOfType);
if (ex_handlers![cur_block].LastClauseType() == ILExceptionBlock.FILTER_START)
{
if (exceptionType != null)
Expand Down Expand Up @@ -446,7 +446,7 @@ public virtual LocalBuilder DeclareLocal(Type localType, bool pinned)
{
ArgumentNullException.ThrowIfNull(localType);
if (localType.IsUserType)
throw new NotSupportedException("User defined subclasses of System.Type are not yet supported.");
throw new NotSupportedException(SR.PlatformNotSupported_UserDefinedSubclassesOfType);
LocalBuilder res = new LocalBuilder(localType, this);
res.is_pinned = pinned;

Expand Down Expand Up @@ -797,7 +797,7 @@ public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optio
{
if ((methodInfo.CallingConvention & CallingConventions.VarArgs) == 0)
{
throw new InvalidOperationException("Method is not VarArgs method and optional types were passed");
throw new InvalidOperationException(SR.InvalidOperation_NotAVarArgCallingConvention);
}

int token = token_gen.GetToken(methodInfo, optionalParameterTypes);
Expand Down Expand Up @@ -901,7 +901,7 @@ public virtual void ThrowException([DynamicallyAccessedMembers(DynamicallyAccess
throw new ArgumentException(SR.Argument_NotExceptionType, nameof(excType));
ConstructorInfo? ctor = excType.GetConstructor(Type.EmptyTypes);
if (ctor == null)
throw new ArgumentException(SR.Argument_MissingDefaultConstructor, nameof(excType));
throw new ArgumentException(SR.Arg_NoDefCTorWithoutTypeName, nameof(excType));
Emit(OpCodes.Newobj, ctor);
Emit(OpCodes.Throw);
}
Expand All @@ -917,7 +917,7 @@ internal void label_fixup(MethodBase mb)
for (int i = 0; i < num_fixups; ++i)
{
if (labels![fixups![i].label_idx].addr < 0)
throw new ArgumentException(string.Format("Label #{0} is not marked in method `{1}'", fixups[i].label_idx + 1, mb.Name));
throw new ArgumentException(SR.Format(SR.Argument_LabelUnmarked, fixups[i].label_idx + 1, mb.Name));
// Diff is the offset from the end of the jump instruction to the address of the label
int diff = labels[fixups[i].label_idx].addr - (fixups[i].pos + fixups[i].offset);
if (fixups[i].offset == 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public override bool ContainsGenericParameters
public override MethodInfo MakeGenericMethod(params Type[] typeArgs)
{
if (!_method.IsGenericMethodDefinition || (_typeArguments != null))
throw new InvalidOperationException("Method is not a generic method definition");
throw new InvalidOperationException(SR.Argument_NeedGenericMethodDefinition);

ArgumentNullException.ThrowIfNull(typeArgs);

if (_method.GetGenericArguments().Length != typeArgs.Length)
throw new ArgumentException("Incorrect length", nameof(typeArgs));
throw new ArgumentException(SR.Format(SR.Argument_NotEnoughGenArguments, _method.GetGenericArguments().Length, typeArgs.Length));

foreach (Type type in typeArgs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ internal RuntimeAssemblyBuilder(AssemblyName n, AssemblyBuilderAccess access)
aname = (AssemblyName)n.Clone();

if (!Enum.IsDefined(typeof(AssemblyBuilderAccess), access))
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
"Argument value {0} is not valid.", (int)access),
throw new ArgumentException(SR.Format(CultureInfo.InvariantCulture,
SR.Arg_EnumIllegalVal, (int)access),
nameof(access));

name = n.Name;
Expand Down
Loading

0 comments on commit 830d3d0

Please sign in to comment.