Skip to content

Commit

Permalink
err: Add error details on cmd failure for bot owners
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Dec 12, 2024
1 parent e839114 commit 914889a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Commands/Err.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public static async Task ErrCmd(SlashCommandContext ctx, [Parameter("code"), Des
var cmd = $"ssh -o ConnectTimeout=30 {Program.ConfigJson.Err.SshUsername}@{Program.ConfigJson.Err.SshHost} \"$PSStyle.OutputRendering = \"PlainText\"; C:\\err.exe {code} 2>&1 | Out-String\"";
var result = await EvalCommands.RunCommand(cmd);

var response = result.ExitCode switch
{
0 or 1 => $"```\n{result.Output}\n```",
_ => "Something went wrong! Please try again."
};
var response = GetErrorMessage(ctx.User.Id, result);

var outMsg = new DiscordMessageBuilder();
if (response.Length < 2000)
Expand All @@ -68,4 +64,19 @@ public static async Task ErrCmd(SlashCommandContext ctx, [Parameter("code"), Des

await ctx.FollowupAsync(outMsg);
}

private static string GetErrorMessage(ulong executingUserId, ShellCommandResponse result)
{
// Success; exit code 1 = msft error lookup tool couldn't find the error code
if (result.ExitCode is 0 or 1)
return $"```\n{result.Output}\n```";

// Failure; show more detail to bot owners
if (Program.Discord.CurrentApplication.Owners.Any(x => x.Id == executingUserId))
return $"Error lookup failed with exit code `{result.ExitCode}`: {(string.IsNullOrWhiteSpace(result.Output) ? "[no output]" : "\n```\n{result.Output}\n```")}";

// Failure; show friendly/generic message to other users
return "Something went wrong! Please try again.";

}
}

0 comments on commit 914889a

Please sign in to comment.