Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): show token creation stack trace upon resolve error #2886

Merged
merged 4 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/@aws-cdk/cdk/lib/lazy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createStackTrace } from './private/stack-trace';
import { IResolvable, IResolveContext } from "./resolvable";
import { Token } from "./token";

Expand Down Expand Up @@ -119,8 +120,13 @@ export class Lazy {
}

abstract class LazyBase implements IResolvable {
public abstract resolve(context: IResolveContext): any;
public readonly creationStack: string[];

constructor() {
this.creationStack = createStackTrace();
}

public abstract resolve(context: IResolveContext): any;
public toString() {
return Token.asString(this);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/cdk/lib/private/intrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class Intrinsic implements IResolvable {
/**
* The captured stack trace which represents the location in which this token was created.
*/
protected readonly trace: string[];
public readonly creationStack: string[];

private readonly value: any;

Expand All @@ -25,7 +25,7 @@ export class Intrinsic implements IResolvable {
throw new Error(`Argument to Intrinsic must be a plain value object, got ${value}`);
}

this.trace = createStackTrace();
this.creationStack = createStackTrace();
this.value = value;
}

Expand Down Expand Up @@ -68,7 +68,7 @@ export class Intrinsic implements IResolvable {
* @param message Error message
*/
protected newError(message: string): any {
return new Error(`${message}\nToken created:\n at ${this.trace.join('\n at ')}\nError thrown:`);
return new Error(`${message}\nToken created:\n at ${this.creationStack.join('\n at ')}\nError thrown:`);
}
}

Expand Down
31 changes: 22 additions & 9 deletions packages/@aws-cdk/cdk/lib/resolvable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export interface IResolveContext {
* Tokens are special objects that participate in synthesis.
*/
export interface IResolvable {
/**
* The creation stack of this resolvable which will be appended to errors
* thrown during resolution.
*/
readonly creationStack?: string[];

/**
* Produce the Token's value at resolution time
*/
Expand Down Expand Up @@ -113,14 +119,21 @@ export class DefaultTokenResolver implements ITokenResolver {
* then finally post-process it.
*/
public resolveToken(t: IResolvable, context: IResolveContext, postProcessor: IPostProcessor) {
let resolved = t.resolve(context);

// The token might have returned more values that need resolving, recurse
resolved = context.resolve(resolved);

resolved = postProcessor.postProcess(resolved, context);

return resolved;
try {
let resolved = t.resolve(context);

// The token might have returned more values that need resolving, recurse
resolved = context.resolve(resolved);
resolved = postProcessor.postProcess(resolved, context);
return resolved;
} catch (e) {
let message = `Resolution error: ${e.message}.`;
if (t.creationStack && t.creationStack.length > 0) {
message += `\nObject creation stack:\n at ${t.creationStack.join('\n at ')}`;
}

throw new Error(message);
}
}

/**
Expand All @@ -145,4 +158,4 @@ export class DefaultTokenResolver implements ITokenResolver {

return fragments.mapTokens({ mapToken: context.resolve }).firstValue;
}
}
}
19 changes: 18 additions & 1 deletion packages/@aws-cdk/cdk/test/test.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export = {
function fn2() {
class ExposeTrace extends Intrinsic {
public get creationTrace() {
return this.trace;
return this.creationStack;
}
}

Expand Down Expand Up @@ -582,6 +582,23 @@ export = {

return tests;
})(),

'creation stack is attached to errors emitted during resolve'(test: Test) {
function showMeInTheStackTrace() {
return Lazy.stringValue({ produce: () => { throw new Error('fooError'); } });
}

const x = showMeInTheStackTrace();
let message;
try {
resolve(x);
} catch (e) {
message = e.message;
}

test.ok(message && message.includes('showMeInTheStackTrace'));
test.done();
}
};

class Promise2 implements IResolvable {
Expand Down