Skip to content

Commit a062f58

Browse files
committed
JSON value access error in submission and editing methods
Multiple methods related to submission or editing had similar errors when attempting to access a returned JSON object. Many tried to get a root node named "json" from a json object. No such node existed (or was already parsed out in the WebAgent's ExecuteRequestAsync method) and so an exception was thrown. Many incorrectly parsed any returned ratelimit error (the "You are doing that too much." error). Related to the ratelimit error, a few of the methods incorrectly checked if the json object returned any errors.
1 parent 07b676d commit a062f58

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

RedditSharp/Reddit.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ public async Task<LiveUpdateEvent> CreateLiveEventAsync(string title, string des
229229
nsfw = nsfw
230230
}).ConfigureAwait(false);
231231

232-
if (json["json"]["errors"].Any())
233-
throw new Exception(json["json"]["errors"][0][0].ToString());
232+
if (json["errors"].Any())
233+
throw new Exception(json["errors"][0][0].ToString());
234234

235-
var id = json["json"]["data"]["id"].ToString();
235+
var id = json["data"]["id"].ToString();
236236

237237
return await GetLiveEvent(new Uri(String.Format(GetLiveEventUrl, id))).ConfigureAwait(false);
238238
}

RedditSharp/Things/Comment.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
89

910
namespace RedditSharp.Things
@@ -176,16 +177,26 @@ public async Task<Comment> ReplyAsync(string message)
176177
// TODO actual error handling. This just hides the error and returns null
177178
//try
178179
//{
179-
var json = await WebAgent.Post(CommentUrl, new
180+
var json = await WebAgent.Post(CommentUrl, new
181+
{
182+
text = message,
183+
thing_id = FullName,
184+
api_type = "json"
185+
//r = Subreddit
186+
}).ConfigureAwait(false);
187+
if (json["errors"].Any())
188+
{
189+
if (json["errors"][0].Any(x => x.ToString() == "RATELIMIT" || x.ToString() == "ratelimit"))
180190
{
181-
text = message,
182-
thing_id = FullName,
183-
api_type = "json"
184-
//r = Subreddit
185-
}).ConfigureAwait(false);
186-
if (json["json"]["ratelimit"] != null)
187-
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
188-
return new Comment(WebAgent, json["json"]["data"]["things"][0], this);
191+
var timeToReset = TimeSpan.FromMinutes(Convert.ToDouble(Regex.Match(json["errors"][0].ElementAt(1).ToString(), @"\d+").Value));
192+
throw new RateLimitException(timeToReset);
193+
}
194+
else
195+
{
196+
throw new Exception(json["errors"][0][0].ToString());
197+
}
198+
}
199+
return new Comment(WebAgent, json["data"]["things"][0], this);
189200
//}
190201
//catch (HttpRequestException ex)
191202
//{
@@ -206,10 +217,10 @@ public async Task EditTextAsync(string newText)
206217
text = newText,
207218
thing_id = FullName
208219
}).ConfigureAwait(false);
209-
if (!json["json"]["errors"].Any())
220+
if (!json["errors"].Any())
210221
Body = newText;
211222
else
212-
throw new Exception($"Errors editing text {json["json"]["errors"][0][0].ToString()}");
223+
throw new Exception($"Errors editing text {json["errors"][0][0].ToString()}");
213224
}
214225

215226
/// <inheritdoc />

RedditSharp/Things/LiveUpdateEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public async Task<bool> EditAsync(string title, string description, string resou
196196

197197
var data = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
198198
JToken json = JToken.Parse(data);
199-
if (!json["json"]["errors"].Any())
199+
if (json["success"].Value<Boolean>())
200200
{
201201
Title = title ?? "";
202202
Description = description ?? "";

RedditSharp/Things/Post.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Reactive.Linq;
88
using System.Threading.Tasks;
9+
using System.Text.RegularExpressions;
910

1011
namespace RedditSharp.Things
1112
{
@@ -143,9 +144,20 @@ public async Task<Comment> CommentAsync(string message)
143144
thing_id = FullName,
144145
api_type = "json"
145146
}).ConfigureAwait(false);
146-
if (json["json"]["ratelimit"] != null)
147-
throw new RateLimitException(TimeSpan.FromSeconds(json["json"]["ratelimit"].ValueOrDefault<double>()));
148-
return new Comment(WebAgent, json["json"]["data"]["things"][0], this);
147+
if (json["errors"].Any())
148+
{
149+
if (json["errors"][0].Any(x => x.ToString() == "RATELIMIT" || x.ToString() == "ratelimit"))
150+
{
151+
var timeToReset = TimeSpan.FromMinutes(Convert.ToDouble(Regex.Match(json["errors"][0].ElementAt(1).ToString(), @"\d+").Value));
152+
throw new RateLimitException(timeToReset);
153+
}
154+
else
155+
{
156+
throw new Exception(json["errors"][0][0].ToString());
157+
}
158+
}
159+
160+
return new Comment(WebAgent, json["data"]["things"][0], this);
149161
}
150162

151163
private async Task<JToken> SimpleActionToggleAsync(string endpoint, bool value, bool requiresModAction = false)
@@ -204,7 +216,7 @@ public async Task EditTextAsync(string newText)
204216
text = newText,
205217
thing_id = FullName
206218
}).ConfigureAwait(false);
207-
if (json["json"].ToString().Contains("\"errors\": []"))
219+
if (!json["errors"].Any())
208220
SelfText = newText;
209221
else
210222
throw new Exception("Error editing text.");
@@ -319,7 +331,7 @@ public IAsyncEnumerable<Comment> EnumerateCommentTreeAsync(int limitPerRequest =
319331
{
320332
return new CommentsEnumarable(WebAgent, this, limitPerRequest);
321333
}
322-
#region Static Operations
334+
#region Static Operations
323335
/// <summary>
324336
/// Sets flair of given post by <paramref name="fullname"/>
325337
/// </summary>

RedditSharp/Things/Subreddit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ private async Task<Post> SubmitAsync(SubmitData data, ICaptchaSolver solver = nu
832832
throw new CaptchaFailedException("Captcha required but not ICaptchaSolver provided");
833833
}
834834

835-
data.Iden = json["json"]["captcha"].ToString();
835+
data.Iden = json["captcha"].ToString();
836836
CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(data.Iden));
837837

838838
// We throw exception due to this method being expected to return a valid Post object, but we cannot

0 commit comments

Comments
 (0)