Skip to content

Commit

Permalink
fix: bundler silently fails in case of an error (#239)
Browse files Browse the repository at this point in the history
Fixes #237
  • Loading branch information
mrgrain authored Oct 29, 2022
1 parent a722cc6 commit 3a63486
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'aws-cdk-lib';
import { EsbuildProvider } from './esbuild-provider';
import { BuildOptions } from './esbuild-types';
import { errorHasCode } from './utils';
import { isEsbuildError } from './utils';

/**
* A path or list or map of paths to the entry points of your code.
Expand Down Expand Up @@ -205,9 +205,10 @@ export class EsbuildBundler {
...this.getOutputOptions(outputDir, { normalize, join }),
});
} catch (error) {
if (errorHasCode(error, 'MODULE_NOT_FOUND')) {
throw error;
if (isEsbuildError(error)) {
throw new Error(`Esbuild failed to bundle ${entryPoints}`);
}
throw error;
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions src/inline-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CodeConfig, InlineCode } from 'aws-cdk-lib/aws-lambda';
import { Construct, Node } from 'constructs';
import { EsbuildProvider } from './esbuild-provider';
import { TransformOptions, Loader } from './esbuild-types';
import { errorHasCode } from './utils';
import { isEsbuildError } from './utils';

/**
* @stability experimental
Expand Down Expand Up @@ -83,10 +83,10 @@ abstract class BaseInlineCode extends InlineCode {

return transformedCode.code;
} catch (error) {
if (errorHasCode(error, 'MODULE_NOT_FOUND')) {
throw error;
if (isEsbuildError(error)) {
throw new Error(`Esbuild failed to transform ${this.constructor.name}`);
}
throw new Error(`Failed to transform ${this.constructor.name}`);
throw error;
}
},
});
Expand Down
10 changes: 3 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
interface CodedError {
code: string;
}

export function errorHasCode(error: unknown, code: string): error is CodedError {
export function isEsbuildError(error: unknown): boolean {
return !!error
&& typeof error == 'object'
&& error != null
&& 'code' in error
&& (error as CodedError).code ==code;
&& 'errors' in error
&& 'warnings' in error;
}
32 changes: 32 additions & 0 deletions test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,38 @@ describe('with an esbuild module path from', () => {
});
});
});

describe('failure cases', () => {
it('will report issues with the module', () => {
providerSpy.mockReturnValue(undefined as any);
expect(() => {
const code = new TypeScriptCode('fixtures/handlers/ts-handler.ts', {
buildOptions: { absWorkingDir: resolve(__dirname) },
esbuildModulePath: '/path/provided/by/prop',
});

new Function(stack, 'MyFunction', {
runtime: LambdaRuntime.NODEJS_14_X,
handler: 'index.handler',
code,
});
}).toThrow('TypeError: Cannot read');
});

it('will report bundling failures', () => {
expect(() => {
const code = new JavaScriptCode('fixtures/handlers/invalid-handler.js', {
buildOptions: { absWorkingDir: resolve(__dirname) },
});

new Function(stack, 'MyFunction', {
runtime: LambdaRuntime.NODEJS_14_X,
handler: 'index.handler',
code,
});
}).toThrow('Esbuild failed to bundle fixtures/handlers/invalid-handler.js');
});
});
});

describe('AWS Lambda', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/handlers/invalid-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { invalid } from './util';

export async function handler(): Promisevoid> {
consoe.lmult(3, 4)); // eslint-disable-line no-console
}
4 changes: 2 additions & 2 deletions test/inline-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('using transformerProps', () => {
expect(() => {
const code = new InlineTypeScriptCode('let : d ===== 1');
code.bind(new Stack());
}).toThrowError('Failed to transform InlineTypeScriptCode');
}).toThrowError('Esbuild failed to transform InlineTypeScriptCode');
});

// Currently no way to capture esbuild output,
Expand All @@ -145,7 +145,7 @@ describe('using transformerProps', () => {
expect(() => {
const code = new InlineTypeScriptCode('let : d ===== 1');
code.bind(new Stack());
}).toThrowError('Failed to transform InlineTypeScriptCode');
}).toThrowError('Esbuild failed to transform InlineTypeScriptCode');

expect(processStdErrWriteSpy).toBeCalledWith(expect.stringContaining('Unexpected "=="'));

Expand Down

0 comments on commit 3a63486

Please sign in to comment.