-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNinaError.cs
82 lines (78 loc) · 2.33 KB
/
NinaError.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// #define MODE_DEBUG
using System.Reflection;
namespace Nina;
public struct NinaErrorPosition {
public string file;
public int line, col;
public NinaErrorPosition(string _file, int _line, int _col) {
file = _file;
line = _line;
col = _col;
}
}
public static class NinaError {
public const string header = "\n[Nina 错误]\n";
public static string trim_header(string _str) {
return _str.StartsWith(header)
? _str.Substring(header.Length)
: _str;
}
public static string gen_report(
string _msg, int _uniqueCode,
List<NinaErrorPosition> _posList) {
string err = header;
if (_uniqueCode >= 0)
err += "(#" + _uniqueCode + ") ";
string msg = _msg;
err += trim_header(msg);
for (int i = 0; i < _posList.Count; ++ i) {
NinaErrorPosition v = _posList[i];
int linei = v.line + 1;
int coli = v.col + 1;
string file = v.file;
string line = linei >= 0 ? linei.ToString() : "(未知)";
string col = coli >= 0 ? coli.ToString() : "(未知)";
err += "\n\t位于 " + file + " (约)第 " + line + " 行";
}
return err;
}
public static void error(string _msg, int _uniqueCode,
List<NinaErrorPosition> _posList) {
string err = gen_report(
_msg, _uniqueCode,
_posList
);
throw new Exception(err);
}
public static void error(string _msg, int _uniqueCode,
NinaErrorPosition? _pos = null) {
error(
_msg, _uniqueCode,
_pos == null
? new List<NinaErrorPosition>()
: new List<NinaErrorPosition>() {
(NinaErrorPosition) _pos !
}
);
}
public static void env(Action _act) {
try {
_act();
}
#if ! MODE_DEBUG
catch (TargetInvocationException tex) {
Console.WriteLine(tex.InnerException!.Message);
}
#endif
catch (Exception ex) {
#if ! MODE_DEBUG
Console.WriteLine(ex.Message);
#else
Console.WriteLine(ex.ToString());
#endif
}
finally {
Environment.Exit(- 1);
}
}
}