From 665dde2e5cfc0e1089f8b26d7032ec79e07f1247 Mon Sep 17 00:00:00 2001 From: Bowen Ni Date: Thu, 19 Jan 2017 10:20:04 -0800 Subject: [PATCH] refactor(compiler): improve error messages in aot compiler (#14017) Previously aot compiler prints stack traces when it fails to resolve. New behavior: aot compiler outputs the error message. Example: https://gist.github.com/bowenni/a7fe81d916e8cd4a06b0e133436f40fb PR Close #14017 --- .../compiler/src/aot/static_reflector.ts | 3 ++- .../compiler/test/aot/static_reflector_spec.ts | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/modules/@angular/compiler/src/aot/static_reflector.ts b/modules/@angular/compiler/src/aot/static_reflector.ts index 4844e245f0..da7cda759d 100644 --- a/modules/@angular/compiler/src/aot/static_reflector.ts +++ b/modules/@angular/compiler/src/aot/static_reflector.ts @@ -9,6 +9,7 @@ import {Attribute, Component, ContentChild, ContentChildren, Directive, Host, HostBinding, HostListener, Inject, Injectable, Input, NgModule, Optional, Output, Pipe, Self, SkipSelf, ViewChild, ViewChildren, animate, group, keyframes, sequence, state, style, transition, trigger} from '@angular/core'; import {ReflectorReader} from '../private_import_core'; +import {SyntaxError} from '../util'; import {StaticSymbol} from './static_symbol'; import {StaticSymbolResolver} from './static_symbol_resolver'; @@ -554,7 +555,7 @@ export class StaticReflector implements ReflectorReader { if (e.fileName) { throw positionalError(message, e.fileName, e.line, e.column); } - throw new Error(message); + throw new SyntaxError(message); } } diff --git a/modules/@angular/compiler/test/aot/static_reflector_spec.ts b/modules/@angular/compiler/test/aot/static_reflector_spec.ts index 3e02898cb4..4d7d657980 100644 --- a/modules/@angular/compiler/test/aot/static_reflector_spec.ts +++ b/modules/@angular/compiler/test/aot/static_reflector_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost} from '@angular/compiler'; +import {StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StaticSymbolResolverHost, SyntaxError} from '@angular/compiler'; import {HostListener, Inject, animate, group, keyframes, sequence, state, style, transition, trigger} from '@angular/core'; import {MockStaticSymbolResolverHost, MockSummaryResolver} from './static_symbol_resolver_spec'; @@ -344,6 +344,20 @@ describe('StaticReflector', () => { 'Recursion not supported, resolving symbol recursive in /tmp/src/function-recursive.d.ts, resolving symbol recursion in /tmp/src/function-reference.ts, resolving symbol in /tmp/src/function-reference.ts')); }); + it('should throw a SyntaxError without stack trace when the required resource cannot be resolved', + () => { + expect( + () => simplify( + reflector.getStaticSymbol('/tmp/src/function-reference.ts', 'AppModule'), ({ + __symbolic: 'error', + message: + 'Could not resolve ./does-not-exist.component relative to /tmp/src/function-reference.ts' + }))) + .toThrowError( + SyntaxError, + 'Error encountered resolving symbol values statically. Could not resolve ./does-not-exist.component relative to /tmp/src/function-reference.ts, resolving symbol AppModule in /tmp/src/function-reference.ts'); + }); + it('should record data about the error in the exception', () => { let threw = false; try {