BGC Tools
Static Public Member Functions | Static Private Member Functions
BGC.IO.FileWriter Class Reference

Static Public Member Functions

static bool WriteJson (string path, Func< JsonObject > createJson, bool pretty=false, Action failCallback=null, Action< string, Exception > overrideExceptionHandling=null)
 

Static Private Member Functions

static void HandleException (string path, Exception excp)
 
static void HandleExceptionFile (string path, string[] errorMessage)
 

Detailed Description

Definition at line 9 of file FileWriter.cs.

Member Function Documentation

◆ HandleException()

static void BGC.IO.FileWriter.HandleException ( string  path,
Exception  excp 
)
inlinestaticprivate

Definition at line 44 of file FileWriter.cs.

45  {
46  string[] errorMessage;
47 
48  if (excp is JsonSerializationException)
49  {
50  errorMessage = new string[]
51  {
52  "JSON serialization error.",
53  "",
54  excp.Message
55  };
56  }
57  else
58  {
59  errorMessage = new string[]
60  {
61  "Exception Encountered",
62  "",
63  excp.Message,
64  };
65  }
66 
67  HandleExceptionFile(path, errorMessage);
68  }
static void HandleExceptionFile(string path, string[] errorMessage)
Definition: FileWriter.cs:70
The exception that is thrown when a JSON value cannot be serialized.

◆ HandleExceptionFile()

static void BGC.IO.FileWriter.HandleExceptionFile ( string  path,
string []  errorMessage 
)
inlinestaticprivate

Definition at line 70 of file FileWriter.cs.

71  {
72  string fileName = Path.GetFileName(path);
73 
74  string directoryPath = Path.GetDirectoryName(path);
75  string directoryName = Path.GetFileName(directoryPath);
76  string errorDirectory = $"Error{directoryName}";
77  string errorDirectoryPath = Path.Combine(Path.GetDirectoryName(directoryPath), errorDirectory);
78  string errorLogPath = Path.Combine(errorDirectoryPath, $"{fileName}_error.txt");
79 
80  if (!Directory.Exists(errorDirectoryPath))
81  {
82  Directory.CreateDirectory(errorDirectoryPath);
83  }
84 
85  if (File.Exists(errorLogPath))
86  {
87  File.Delete(errorLogPath);
88  }
89 
90  File.WriteAllLines(errorLogPath, errorMessage);
91  }

◆ WriteJson()

static bool BGC.IO.FileWriter.WriteJson ( string  path,
Func< JsonObject createJson,
bool  pretty = false,
Action  failCallback = null,
Action< string, Exception overrideExceptionHandling = null 
)
inlinestatic

Definition at line 11 of file FileWriter.cs.

Referenced by BGC.Audio.Calibration.Serialize(), BGC.Users.ProfileData.Serialize(), and BGC.Study.ProtocolManager.SerializeAll().

17  {
18  bool wroteToFile = false;
19 
20  try
21  {
22  File.WriteAllText(path, createJson().ToString(pretty));
23  wroteToFile = true;
24  }
25  catch (Exception excp)
26  {
27  Debug.LogError($"Error serializing {path}: {excp.Message}");
28 
29  failCallback?.Invoke();
30 
31  if (overrideExceptionHandling != null)
32  {
33  overrideExceptionHandling.Invoke(path, excp);
34  }
35  else
36  {
37  HandleException(path, excp);
38  }
39  }
40 
41  return wroteToFile;
42  }
static void HandleException(string path, Exception excp)
Definition: FileWriter.cs:44
Here is the caller graph for this function:

The documentation for this class was generated from the following file: