-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThrow.cs
149 lines (128 loc) · 5.85 KB
/
Throw.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.Serialization;
namespace Cesil
{
internal static class Throw
{
private const string UNKNOWN_FILE = "<unknown file>";
private const string UNKNOWN_MEMBER = "<unknown member>";
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ImpossibleException<T, V>(
string message,
IBoundConfiguration<V> config,
[CallerFilePath]
string? file = null,
[CallerMemberName]
string? member = null,
[CallerLineNumber]
int line = -1)
=> throw Cesil.ImpossibleException.Create(message, file ?? UNKNOWN_FILE, member ?? UNKNOWN_MEMBER, line, config);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ImpossibleException<T>(
string message,
Options options,
[CallerFilePath]
string? file = null,
[CallerMemberName]
string? member = null,
[CallerLineNumber]
int line = -1)
=> throw Cesil.ImpossibleException.Create(message, file ?? UNKNOWN_FILE, member ?? UNKNOWN_MEMBER, line, options);
// prefer the other two impossible throwers to this one
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ImpossibleException<T>(
string message,
[CallerFilePath]
string? file = null,
[CallerMemberName]
string? member = null,
[CallerLineNumber]
int line = -1)
=> throw Cesil.ImpossibleException.Create(message, file ?? UNKNOWN_FILE, member ?? UNKNOWN_MEMBER, line);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T InvalidOperationException<T>(string message)
=> throw new InvalidOperationException(message);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T SerializationException<T>(string message)
=> throw new SerializationException(message);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ArgumentException<T>(string message, string name)
=> throw new ArgumentException(message, name);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ArgumentNullException<T>(string name)
=> throw new ArgumentNullException(name);
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ObjectDisposedException<T>(string typeName)
=> throw new ObjectDisposedException($"Instance of {typeName}");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ArgumentOutOfRangeException<T>(string paramName, int val, int upperExclusive)
=> throw new ArgumentOutOfRangeException(paramName, $"Expected between 0 and {upperExclusive}, was {val}");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ArgumentOutOfRangeException<T>(string paramName, Index ix, int effective, int upperExclusive)
=> throw new ArgumentOutOfRangeException(paramName, $"Expected Index between 0 and {upperExclusive}, was {effective} ({ix})");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T ArgumentOutOfRangeException<T>(string paramName, Range r, int effectiveStart, int effectiveEnd, int upperExclusive)
=> throw new ArgumentOutOfRangeException(paramName, $"Expected range end points to be between 0 and {upperExclusive}, was {effectiveStart}..{effectiveEnd} ({r})");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T KeyNotFoundException<T>(string key)
=> throw new KeyNotFoundException($"Key was {key}");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T NotSupportedException<T>(string type, string method)
=> throw new NotSupportedException($"Method {method} on {type} is not supported");
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T OperationCanceledException<T>()
=> throw new OperationCanceledException();
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T PoisonAndRethrow<T>(PoisonableBase toPoison, Exception e)
{
toPoison.SetPoison(e);
// this preserves stack traces in a way `throw e` doesn't.
//
// in "normal" code we'd just re-throw with a naked `throw`, but
// we don't want any throws outside of this class for other reasons.
//
// automating poisoning is a nice bonus
var wrapped = ExceptionDispatchInfo.Capture(e);
wrapped.Throw();
return default;
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void ParseFailed(Parser parser, in ReadContext ctx, ReadOnlySpan<char> data)
{
string msg;
if (ctx.HasColumn)
{
msg = $"Failed to parse \"{new string(data)}\" for column index={ctx.Column} using {parser}";
}
else
{
msg = $"Failed to parse \"{new string(data)}\"using {parser}";
}
throw new SerializationException(msg);
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static T NotImplementedException<T>(string message)
=> throw new NotImplementedException(message);
}
}