Skip to content

Commit c0478c5

Browse files
rest of debug adapter
1 parent dea9d82 commit c0478c5

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

src/PowerShellEditorServices.Engine/Hosting/EditorServicesHost.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void StartLanguageService(
236236
EditorServiceTransportConfig config,
237237
ProfilePaths profilePaths)
238238
{
239-
while (System.Diagnostics.Debugger.IsAttached)
239+
while (!System.Diagnostics.Debugger.IsAttached)
240240
{
241241
System.Console.WriteLine($"{Process.GetCurrentProcess().Id}");
242242
Thread.Sleep(2000);
@@ -295,11 +295,11 @@ public void StartDebugService(
295295
ProfilePaths profilePaths,
296296
bool useExistingSession)
297297
{
298-
while (System.Diagnostics.Debugger.IsAttached)
299-
{
300-
System.Console.WriteLine($"{Process.GetCurrentProcess().Id}");
301-
Thread.Sleep(2000);
302-
}
298+
//while (System.Diagnostics.Debugger.IsAttached)
299+
//{
300+
// System.Console.WriteLine($"{Process.GetCurrentProcess().Id}");
301+
// Thread.Sleep(2000);
302+
//}
303303

304304
_logger.LogInformation($"Debug NamedPipe: {config.InOutPipeName}\nDebug OutPipe: {config.OutPipeName}");
305305

src/PowerShellEditorServices.Engine/Services/DebugAdapter/Handlers/DebuggerActionHandlers.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.PowerShell.EditorServices.Engine.Services;
1212
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
13+
using OmniSharp.Extensions.JsonRpc;
1314

1415
namespace Microsoft.PowerShell.EditorServices.Engine.Handlers
1516
{
@@ -68,8 +69,15 @@ public PauseHandler(
6869

6970
public Task<PauseResponse> Handle(PauseArguments request, CancellationToken cancellationToken)
7071
{
71-
_debugService.Break();
72-
return Task.FromResult(new PauseResponse());
72+
try
73+
{
74+
_debugService.Break();
75+
return Task.FromResult(new PauseResponse());
76+
}
77+
catch(NotSupportedException e)
78+
{
79+
throw new RpcErrorException(0, e.Message);
80+
}
7381
}
7482
}
7583

src/PowerShellEditorServices.Engine/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using Microsoft.Extensions.Logging;
13-
using Microsoft.PowerShell.EditorServices.Engine.Logging;
1413
using Microsoft.PowerShell.EditorServices.Engine.Services;
1514
using Microsoft.PowerShell.EditorServices.Engine.Services.PowerShellContext;
1615
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
@@ -255,7 +254,7 @@ public async Task<Unit> Handle(PsesAttachRequestArguments request, CancellationT
255254
_logger.LogInformation(
256255
$"Attach request aborted, received {request.ProcessId} for processId.");
257256

258-
throw new Exception("User aborted attach to PowerShell host process.");
257+
throw new RpcErrorException(0, "User aborted attach to PowerShell host process.");
259258
}
260259

261260
StringBuilder errorMessages = new StringBuilder();
@@ -264,11 +263,11 @@ public async Task<Unit> Handle(PsesAttachRequestArguments request, CancellationT
264263
{
265264
if (runspaceVersion.Version.Major < 4)
266265
{
267-
throw new Exception($"Remote sessions are only available with PowerShell 4 and higher (current session is {runspaceVersion.Version}).");
266+
throw new RpcErrorException(0, $"Remote sessions are only available with PowerShell 4 and higher (current session is {runspaceVersion.Version}).");
268267
}
269268
else if (_powerShellContextService.CurrentRunspace.Location == RunspaceLocation.Remote)
270269
{
271-
throw new Exception($"Cannot attach to a process in a remote session when already in a remote session.");
270+
throw new RpcErrorException(0, $"Cannot attach to a process in a remote session when already in a remote session.");
272271
}
273272

274273
await _powerShellContextService.ExecuteScriptStringAsync(
@@ -277,7 +276,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
277276

278277
if (errorMessages.Length > 0)
279278
{
280-
throw new Exception($"Could not establish remote session to computer '{request.ComputerName}'");
279+
throw new RpcErrorException(0, $"Could not establish remote session to computer '{request.ComputerName}'");
281280
}
282281

283282
_debugStateService.IsRemoteAttach = true;
@@ -287,7 +286,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
287286
{
288287
if (runspaceVersion.Version.Major < 5)
289288
{
290-
throw new Exception($"Attaching to a process is only available with PowerShell 5 and higher (current session is {runspaceVersion.Version}).");
289+
throw new RpcErrorException(0, $"Attaching to a process is only available with PowerShell 5 and higher (current session is {runspaceVersion.Version}).");
291290
}
292291

293292
await _powerShellContextService.ExecuteScriptStringAsync(
@@ -296,14 +295,14 @@ await _powerShellContextService.ExecuteScriptStringAsync(
296295

297296
if (errorMessages.Length > 0)
298297
{
299-
throw new Exception($"Could not attach to process '{processId}'");
298+
throw new RpcErrorException(0, $"Could not attach to process '{processId}'");
300299
}
301300
}
302301
else if (customPipeNameIsSet)
303302
{
304303
if (runspaceVersion.Version < s_minVersionForCustomPipeName)
305304
{
306-
throw new Exception($"Attaching to a process with CustomPipeName is only available with PowerShell 6.2 and higher (current session is {runspaceVersion.Version}).");
305+
throw new RpcErrorException(0, $"Attaching to a process with CustomPipeName is only available with PowerShell 6.2 and higher (current session is {runspaceVersion.Version}).");
307306
}
308307

309308
await _powerShellContextService.ExecuteScriptStringAsync(
@@ -312,15 +311,15 @@ await _powerShellContextService.ExecuteScriptStringAsync(
312311

313312
if (errorMessages.Length > 0)
314313
{
315-
throw new Exception($"Could not attach to process with CustomPipeName: '{request.CustomPipeName}'");
314+
throw new RpcErrorException(0, $"Could not attach to process with CustomPipeName: '{request.CustomPipeName}'");
316315
}
317316
}
318317
else if (request.ProcessId != "current")
319318
{
320319
_logger.LogError(
321320
$"Attach request failed, '{request.ProcessId}' is an invalid value for the processId.");
322321

323-
throw new Exception("A positive integer must be specified for the processId field.");
322+
throw new RpcErrorException(0, "A positive integer must be specified for the processId field.");
324323
}
325324

326325
// Clear any existing breakpoints before proceeding
@@ -343,7 +342,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
343342
_logger.LogError(
344343
$"Attach request failed, '{request.RunspaceId}' is an invalid value for the processId.");
345344

346-
throw new Exception("A positive integer must be specified for the RunspaceId field.");
345+
throw new RpcErrorException(0, "A positive integer must be specified for the RunspaceId field.");
347346
}
348347

349348
debugRunspaceCmd = $"\nDebug-Runspace -Id {runspaceId}";

src/PowerShellEditorServices.Engine/Services/DebugAdapter/Handlers/SetVariableHandler.cs

+20-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Linq;
8+
using System.Management.Automation;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.Extensions.Logging;
@@ -13,6 +14,7 @@
1314
using Microsoft.PowerShell.EditorServices.Utility;
1415
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
1516
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
17+
using OmniSharp.Extensions.JsonRpc;
1618

1719
namespace Microsoft.PowerShell.EditorServices.Engine.Handlers
1820
{
@@ -31,8 +33,8 @@ public SetVariableHandler(
3133

3234
public async Task<SetVariableResponse> Handle(SetVariableArguments request, CancellationToken cancellationToken)
3335
{
34-
//try
35-
//{
36+
try
37+
{
3638
string updatedValue =
3739
await _debugService.SetVariableAsync(
3840
(int) request.VariablesReference,
@@ -44,23 +46,22 @@ await _debugService.SetVariableAsync(
4446
Value = updatedValue
4547
};
4648

47-
// TODO: Bring back maybe if Omnisharp lib supports a SendErrorAsync equivalent.
48-
//}
49-
//catch (Exception ex) when (ex is ArgumentTransformationMetadataException ||
50-
// ex is InvalidPowerShellExpressionException ||
51-
// ex is SessionStateUnauthorizedAccessException)
52-
//{
53-
// // Catch common, innocuous errors caused by the user supplying a value that can't be converted or the variable is not settable.
54-
// Logger.Write(LogLevel.Verbose, $"Failed to set variable: {ex.Message}");
55-
// await requestContext.SendErrorAsync(ex.Message);
56-
//}
57-
//catch (Exception ex)
58-
//{
59-
// Logger.Write(LogLevel.Error, $"Unexpected error setting variable: {ex.Message}");
60-
// string msg =
61-
// $"Unexpected error: {ex.GetType().Name} - {ex.Message} Please report this error to the PowerShellEditorServices project on GitHub.";
62-
// await requestContext.SendErrorAsync(msg);
63-
//}
49+
}
50+
catch (Exception ex) when(ex is ArgumentTransformationMetadataException ||
51+
ex is InvalidPowerShellExpressionException ||
52+
ex is SessionStateUnauthorizedAccessException)
53+
{
54+
// Catch common, innocuous errors caused by the user supplying a value that can't be converted or the variable is not settable.
55+
_logger.LogTrace($"Failed to set variable: {ex.Message}");
56+
throw new RpcErrorException(0, ex.Message);
57+
}
58+
catch (Exception ex)
59+
{
60+
_logger.LogError($"Unexpected error setting variable: {ex.Message}");
61+
string msg =
62+
$"Unexpected error: {ex.GetType().Name} - {ex.Message} Please report this error to the PowerShellEditorServices project on GitHub.";
63+
throw new RpcErrorException(0, msg);
64+
}
6465
}
6566
}
6667
}

0 commit comments

Comments
 (0)