Skip to content

Commit 6f1ddc0

Browse files
Saqooshaclaude
andcommitted
refactor: Improve parameter validation and safety in GetConsoleLogsResource
- Add bounds validation for offset (≥0) and limit (1-1000) parameters - Simplify parameter extraction with helper method - Add defensive programming for pagination metadata access - Reduce code duplication and improve maintainability Addresses CodeRabbit review feedback for better error handling and robustness. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 925431d commit 6f1ddc0

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

Editor/Resources/GetConsoleLogsResource.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,11 @@ public GetConsoleLogsResource(IConsoleLogsService consoleLogsService)
2626
/// <returns>A JObject containing the list of logs with pagination info</returns>
2727
public override JObject Fetch(JObject parameters)
2828
{
29-
string logType = null;
30-
int offset = 0;
31-
int limit = 100;
29+
string logType = parameters?["logType"]?.ToString();
30+
if (string.IsNullOrWhiteSpace(logType)) logType = null;
3231

33-
if (parameters != null)
34-
{
35-
// Extract logType
36-
if (parameters.ContainsKey("logType") && parameters["logType"] != null)
37-
{
38-
logType = parameters["logType"].ToString()?.ToLowerInvariant();
39-
if (string.IsNullOrWhiteSpace(logType))
40-
{
41-
logType = null;
42-
}
43-
}
44-
45-
// Extract pagination parameters
46-
if (parameters.ContainsKey("offset") && parameters["offset"] != null)
47-
{
48-
int.TryParse(parameters["offset"].ToString(), out offset);
49-
}
50-
51-
if (parameters.ContainsKey("limit") && parameters["limit"] != null)
52-
{
53-
int.TryParse(parameters["limit"].ToString(), out limit);
54-
}
55-
}
32+
int offset = Math.Max(0, GetIntParameter(parameters, "offset", 0));
33+
int limit = Math.Max(1, Math.Min(1000, GetIntParameter(parameters, "limit", 100)));
5634

5735
// Use the new paginated method
5836
JObject result = _consoleLogsService.GetLogsAsJson(logType, offset, limit);
@@ -62,11 +40,27 @@ public override JObject Fetch(JObject parameters)
6240

6341
var pagination = result["pagination"] as JObject;
6442
string typeFilter = logType != null ? $" of type '{logType}'" : "";
65-
result["message"] = $"Retrieved {pagination["returnedCount"]} of {pagination["filteredCount"]} log entries{typeFilter} (offset: {offset}, limit: {limit})";
43+
int returnedCount = pagination?["returnedCount"]?.Value<int>() ?? 0;
44+
int filteredCount = pagination?["filteredCount"]?.Value<int>() ?? 0;
45+
result["message"] = $"Retrieved {returnedCount} of {filteredCount} log entries{typeFilter} (offset: {offset}, limit: {limit})";
6646

6747
return result;
6848
}
6949

50+
/// <summary>
51+
/// Helper method to safely extract integer parameters with default values
52+
/// </summary>
53+
/// <param name="parameters">JObject containing parameters</param>
54+
/// <param name="key">Parameter key to extract</param>
55+
/// <param name="defaultValue">Default value if parameter is missing or invalid</param>
56+
/// <returns>Extracted integer value or default</returns>
57+
private static int GetIntParameter(JObject parameters, string key, int defaultValue)
58+
{
59+
if (parameters?[key] != null && int.TryParse(parameters[key].ToString(), out int value))
60+
return value;
61+
return defaultValue;
62+
}
63+
7064

7165
}
7266
}

0 commit comments

Comments
 (0)