File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Extensions/JTokenExtensions Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
+ using Newtonsoft . Json . Linq ;
2
3
3
4
namespace RedditSharp
4
5
{
@@ -8,6 +9,8 @@ namespace RedditSharp
8
9
[ Serializable ]
9
10
public class RedditException : Exception
10
11
{
12
+ public JArray Errors { get ; }
13
+
11
14
/// <summary>
12
15
/// Initializes a new instance of the RedditException class.
13
16
/// </summary>
@@ -26,6 +29,17 @@ public RedditException(string message)
26
29
27
30
}
28
31
32
+ /// <summary>
33
+ /// Initializes a new instance of the RedditException class with a specified error message and a JArray of errors
34
+ /// </summary>
35
+ /// <param name="message">The message that describes the error.</param>
36
+ /// <param name="errors">List of errors.</param>
37
+ public RedditException ( string message , JArray errors )
38
+ : base ( message )
39
+ {
40
+ Errors = errors ;
41
+ }
42
+
29
43
/// <summary>
30
44
/// Initializes a new instance of the RedditException class with a specified error message and
31
45
/// a referenced inner exception that is the cause of this exception.
Original file line number Diff line number Diff line change
1
+ using Newtonsoft . Json . Linq ;
2
+
3
+ namespace RedditSharp . Extensions . JTokenExtensions
4
+ {
5
+ public static class JTokenExtensions
6
+ {
7
+ public static void ThrowIfHasErrors ( this JToken json , string message )
8
+ {
9
+ if ( json [ "errors" ] . IsNonEmptyArray ( out var errors ) )
10
+ {
11
+ throw new RedditException ( $ "{ message } { errors } ", errors ) ;
12
+ }
13
+ }
14
+
15
+ public static bool IsNonEmptyArray ( this JToken json , out JArray array )
16
+ {
17
+ var isArray = _IsArray ( json , out array ) ;
18
+ return isArray && array . Count > 0 ;
19
+ }
20
+
21
+ private static bool _IsArray ( JToken json , out JArray array )
22
+ {
23
+ if ( json != null && json . Type == JTokenType . Array )
24
+ {
25
+ array = ( JArray ) json ;
26
+ return true ;
27
+ }
28
+ array = default ;
29
+ return false ;
30
+
31
+ }
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments