Skip to content

Commit f0adbc4

Browse files
committed
fix(@angular-devkit/architect): properly report errors thrown by builder
When they are thrown by the builder itself.
1 parent e6ba05b commit f0adbc4

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

packages/angular_devkit/architect/src/create-builder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { experimental, isPromise, json, logging } from '@angular-devkit/core';
9-
import { Observable, Subscription, from, isObservable, of } from 'rxjs';
9+
import { Observable, Subscription, from, isObservable, of, throwError } from 'rxjs';
1010
import { tap } from 'rxjs/operators';
1111
import {
1212
BuilderContext,
1313
BuilderHandlerFn,
1414
BuilderInfo,
1515
BuilderInput,
1616
BuilderOutput,
17+
BuilderOutputLike,
1718
BuilderProgressState,
1819
Target,
1920
TypedBuilderProgress,
@@ -148,7 +149,12 @@ export function createBuilder<
148149
};
149150

150151
context.reportRunning();
151-
let result = fn(i.options as OptT, context);
152+
let result: BuilderOutputLike;
153+
try {
154+
result = fn(i.options as OptT, context);
155+
} catch (e) {
156+
result = throwError(e);
157+
}
152158

153159
if (isPromise(result)) {
154160
result = from(result);

packages/angular_devkit/architect/src/index2_spec.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { schema } from '@angular-devkit/core';
99
import { timer } from 'rxjs';
1010
import { map, take, tap, toArray } from 'rxjs/operators';
1111
import { TestingArchitectHost } from '../testing/testing-architect-host';
12-
import { BuilderOutput } from './api';
12+
import { BuilderOutput, BuilderRun } from './api';
1313
import { Architect } from './architect';
1414
import { createBuilder } from './create-builder';
1515

@@ -135,4 +135,54 @@ describe('architect', () => {
135135
expect(results).toBe(10);
136136
expect(all.length).toBe(10);
137137
});
138+
139+
it('reports errors in the builder', async () => {
140+
testArchitectHost.addBuilder('package:error', createBuilder(() => {
141+
throw new Error('Error in the builder.');
142+
}));
143+
144+
let run: BuilderRun | undefined = undefined;
145+
try {
146+
try {
147+
// This should not throw.
148+
run = await architect.scheduleBuilder('package:error', {});
149+
} catch (err) {
150+
expect(err).toBeUndefined();
151+
throw err;
152+
}
153+
154+
// This should throw.
155+
await run.result;
156+
expect('to throw').not.toEqual('to throw');
157+
} catch {
158+
}
159+
if (run) {
160+
await run.stop();
161+
}
162+
});
163+
164+
it('reports errors in the builder (async)', async () => {
165+
testArchitectHost.addBuilder('package:error', createBuilder(() => {
166+
return new Promise((_, reject) => reject(new Error('Error async')));
167+
}));
168+
169+
let run: BuilderRun | undefined = undefined;
170+
try {
171+
try {
172+
// This should not throw.
173+
run = await architect.scheduleBuilder('package:error', {});
174+
} catch (err) {
175+
expect(err).toBeUndefined();
176+
throw err;
177+
}
178+
179+
// This should throw.
180+
await run.result;
181+
expect('to throw').not.toEqual('to throw');
182+
} catch {
183+
}
184+
if (run) {
185+
await run.stop();
186+
}
187+
});
138188
});

0 commit comments

Comments
 (0)