Skip to content

Commit

Permalink
fix: show a nice error message when requesting async stacks
Browse files Browse the repository at this point in the history
Fixes #353
  • Loading branch information
connor4312 committed Mar 10, 2020
1 parent 7f8f63f commit 80db909
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/adapter/stackTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IPreferredUiLocation } from './sources';
import { RawLocation, Thread } from './threads';
import { IExtraProperty, IScopeRef } from './variables';
import { LogPointCompiler } from './breakpoints/conditions/logPoint';
import { asyncScopesNotAvailable, ProtocolError } from '../dap/errors';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -184,7 +185,9 @@ export class StackFrame {
}

async scopes(): Promise<Dap.ScopesResult> {
if (!this._scope) return { scopes: [] };
if (!this._scope) {
throw new ProtocolError(asyncScopesNotAvailable());
}

const scopes: Dap.Scope[] = [];
for (let scopeNumber = 0; scopeNumber < this._scope.chain.length; scopeNumber++) {
Expand Down
2 changes: 1 addition & 1 deletion src/dap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ export default class Connection {
Number(process.hrtime.bigint() - receivedTime) / 1e6,
);
} catch (e) {
console.error(e);
const format = isExternalError(e)
? e.message
: `Error processing ${msg.command}: ${e.stack || e.message}`;
Expand All @@ -195,6 +194,7 @@ export default class Connection {
body: { error: e.cause },
});
} else {
console.error(e);
this._send({
...response,
success: false,
Expand Down
14 changes: 12 additions & 2 deletions src/dap/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const enum ErrorCodes {
InvalidHitCondition,
InvalidLogPointBreakpointSyntax,
BrowserNotFound,
AsyncScopesNotAvailable,
}

export function reportToConsole(dap: Dap.Api, error: string) {
Expand All @@ -28,11 +29,11 @@ export function reportToConsole(dap: Dap.Api, error: string) {
});
}

export function createSilentError(text: string): Dap.Error {
export function createSilentError(text: string, code = ErrorCodes.SilentError): Dap.Error {
return {
__errorMarker: true,
error: {
id: ErrorCodes.SilentError,
id: code,
format: text,
showUser: false,
},
Expand Down Expand Up @@ -147,6 +148,15 @@ export const browserNotFound = (
export const invalidLogPointSyntax = (error: string) =>
createUserError(error, ErrorCodes.InvalidLogPointBreakpointSyntax);

export const asyncScopesNotAvailable = () =>
createSilentError(
localize(
'asyncScopesNotAvailable',
'Variables not available in async stacks',
ErrorCodes.AsyncScopesNotAvailable,
),
);

export class ProtocolError extends Error {
public readonly cause: Dap.Message;

Expand Down
36 changes: 20 additions & 16 deletions src/test/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,27 @@ export class Logger {
);
if (!withScopes) continue;
const scopes = await this._dap.scopes({ frameId: frame.id });
for (let i = 0; i < scopes.scopes.length; i++) {
const scope = scopes.scopes[i];
if (scope.expensive) {
this._log(` scope #${i}: ${scope.name} [expensive]`);
continue;
if (typeof scopes === 'string') {
this._log(` scope error: ${scopes}`);
} else {
for (let i = 0; i < scopes.scopes.length; i++) {
const scope = scopes.scopes[i];
if (scope.expensive) {
this._log(` scope #${i}: ${scope.name} [expensive]`);
continue;
}
await this.logVariable(
{
name: 'scope #' + i,
value: scope.name,
variablesReference: scope.variablesReference,
namedVariables: scope.namedVariables,
indexedVariables: scope.indexedVariables,
},
{},
' ',
);
}
await this.logVariable(
{
name: 'scope #' + i,
value: scope.name,
variablesReference: scope.variablesReference,
namedVariables: scope.namedVariables,
indexedVariables: scope.indexedVariables,
},
{},
' ',
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/stacks/stacks-async.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ bar @ <eval>/VM<xx>:13:15

----async function----
<anonymous> @ <eval>/VM<xx>:8:11
scope error: Variables not available in async stacks
----setTimeout----
foo @ <eval>/VM<xx>:7:9
scope error: Variables not available in async stacks
bar @ <eval>/VM<xx>:13:15
scope error: Variables not available in async stacks
----async function----
<anonymous> @ <eval>/VM<xx>:15:7
scope error: Variables not available in async stacks
2 changes: 2 additions & 0 deletions src/test/stacks/stacks-cross-target.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

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

0 comments on commit 80db909

Please sign in to comment.