Skip to content

Commit 9421320

Browse files
committed
fix: show a nice error message when requesting async stacks
Fixes #353
1 parent e249fb1 commit 9421320

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

src/adapter/stackTrace.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { IPreferredUiLocation } from './sources';
1010
import { RawLocation, Thread } from './threads';
1111
import { IExtraProperty, IScopeRef } from './variables';
1212
import { LogPointCompiler } from './breakpoints/conditions/logPoint';
13+
import { asyncScopesNotAvailable, ProtocolError } from '../dap/errors';
1314

1415
const localize = nls.loadMessageBundle();
1516

@@ -184,7 +185,9 @@ export class StackFrame {
184185
}
185186

186187
async scopes(): Promise<Dap.ScopesResult> {
187-
if (!this._scope) return { scopes: [] };
188+
if (!this._scope) {
189+
throw new ProtocolError(asyncScopesNotAvailable());
190+
}
188191

189192
const scopes: Dap.Scope[] = [];
190193
for (let scopeNumber = 0; scopeNumber < this._scope.chain.length; scopeNumber++) {

src/dap/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ export default class Connection {
183183
Number(process.hrtime.bigint() - receivedTime) / 1e6,
184184
);
185185
} catch (e) {
186-
console.error(e);
187186
const format = isExternalError(e)
188187
? e.message
189188
: `Error processing ${msg.command}: ${e.stack || e.message}`;
@@ -195,6 +194,7 @@ export default class Connection {
195194
body: { error: e.cause },
196195
});
197196
} else {
197+
console.error(e);
198198
this._send({
199199
...response,
200200
success: false,

src/dap/errors.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const enum ErrorCodes {
1919
InvalidHitCondition,
2020
InvalidLogPointBreakpointSyntax,
2121
BrowserNotFound,
22+
AsyncScopesNotAvailable,
2223
}
2324

2425
export function reportToConsole(dap: Dap.Api, error: string) {
@@ -28,11 +29,11 @@ export function reportToConsole(dap: Dap.Api, error: string) {
2829
});
2930
}
3031

31-
export function createSilentError(text: string): Dap.Error {
32+
export function createSilentError(text: string, code = ErrorCodes.SilentError): Dap.Error {
3233
return {
3334
__errorMarker: true,
3435
error: {
35-
id: ErrorCodes.SilentError,
36+
id: code,
3637
format: text,
3738
showUser: false,
3839
},
@@ -147,6 +148,15 @@ export const browserNotFound = (
147148
export const invalidLogPointSyntax = (error: string) =>
148149
createUserError(error, ErrorCodes.InvalidLogPointBreakpointSyntax);
149150

151+
export const asyncScopesNotAvailable = () =>
152+
createSilentError(
153+
localize(
154+
'asyncScopesNotAvailable',
155+
'Variables not available in async stacks',
156+
ErrorCodes.AsyncScopesNotAvailable,
157+
),
158+
);
159+
150160
export class ProtocolError extends Error {
151161
public readonly cause: Dap.Message;
152162

src/test/logger.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,27 @@ export class Logger {
161161
);
162162
if (!withScopes) continue;
163163
const scopes = await this._dap.scopes({ frameId: frame.id });
164-
for (let i = 0; i < scopes.scopes.length; i++) {
165-
const scope = scopes.scopes[i];
166-
if (scope.expensive) {
167-
this._log(` scope #${i}: ${scope.name} [expensive]`);
168-
continue;
164+
if (typeof scopes === 'string') {
165+
this._log(` scope error: ${scopes}`);
166+
} else {
167+
for (let i = 0; i < scopes.scopes.length; i++) {
168+
const scope = scopes.scopes[i];
169+
if (scope.expensive) {
170+
this._log(` scope #${i}: ${scope.name} [expensive]`);
171+
continue;
172+
}
173+
await this.logVariable(
174+
{
175+
name: 'scope #' + i,
176+
value: scope.name,
177+
variablesReference: scope.variablesReference,
178+
namedVariables: scope.namedVariables,
179+
indexedVariables: scope.indexedVariables,
180+
},
181+
{},
182+
' ',
183+
);
169184
}
170-
await this.logVariable(
171-
{
172-
name: 'scope #' + i,
173-
value: scope.name,
174-
variablesReference: scope.variablesReference,
175-
namedVariables: scope.namedVariables,
176-
indexedVariables: scope.indexedVariables,
177-
},
178-
{},
179-
' ',
180-
);
181185
}
182186
}
183187

src/test/stacks/stacks-async.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ bar @ <eval>/VM<xx>:13:15
1313

1414
----async function----
1515
<anonymous> @ <eval>/VM<xx>:8:11
16+
scope error: Variables not available in async stacks
1617
----setTimeout----
1718
foo @ <eval>/VM<xx>:7:9
19+
scope error: Variables not available in async stacks
1820
bar @ <eval>/VM<xx>:13:15
21+
scope error: Variables not available in async stacks
1922
----async function----
2023
<anonymous> @ <eval>/VM<xx>:15:7
24+
scope error: Variables not available in async stacks

src/test/stacks/stacks-cross-target.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77

88
----postMessage----
99
<anonymous> @ ${workspaceFolder}/web/worker.js:6:5
10+
scope error: Variables not available in async stacks
1011
----Worker.postMessage----
1112
<anonymous> @ <eval>/VM<xx>:1:10
13+
scope error: Variables not available in async stacks

0 commit comments

Comments
 (0)