mirror of
https://github.com/actions/runner.git
synced 2025-12-13 10:05:23 +00:00
Guard against NullReference while creating HostContext (#2343)
* Guard against NullReference while creating HostContext * Throw on IOUtil loading object if content is empty or object is null * removed unused dependency * Changed exceptions to ArgumentNullException and ArgumentException * fixed my mistake on LoadObject * fixed tests * trigger workflows * trigger workflows * encode files using utf8
This commit is contained in:
@@ -40,10 +40,19 @@ namespace GitHub.Runner.Sdk
|
||||
File.WriteAllText(path, StringUtil.ConvertToJson(obj), Encoding.UTF8);
|
||||
}
|
||||
|
||||
public static T LoadObject<T>(string path)
|
||||
public static T LoadObject<T>(string path, bool required = false)
|
||||
{
|
||||
string json = File.ReadAllText(path, Encoding.UTF8);
|
||||
return StringUtil.ConvertFromJson<T>(json);
|
||||
if (required && string.IsNullOrEmpty(json))
|
||||
{
|
||||
throw new ArgumentNullException($"File {path} is empty");
|
||||
}
|
||||
T result = StringUtil.ConvertFromJson<T>(json);
|
||||
if (required && result == null)
|
||||
{
|
||||
throw new ArgumentException("Converting json to object resulted in a null value");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static string GetSha256Hash(string path)
|
||||
|
||||
Reference in New Issue
Block a user