refactor(ivy): i18n - rename `I18nError` to `TranslationParserError` (#32881)

This closer reflects what caused the error.

PR Close #32881
This commit is contained in:
Pete Bacon Darwin 2019-10-08 14:46:29 +01:00 committed by Alex Rickabaugh
parent 2cdb3a079d
commit 90855f331f
6 changed files with 17 additions and 16 deletions

View File

@ -8,10 +8,9 @@
import {ParseErrorLevel, ParseSourceSpan} from '@angular/compiler';
/**
* Instances of this class are thrown when there is an error in the source code that is being
* translated.
* This error is thrown when there is a problem parsing a translation file.
*/
export class I18nError extends Error {
export class TranslationParseError extends Error {
constructor(
public span: ParseSourceSpan, public msg: string,
public level: ParseErrorLevel = ParseErrorLevel.ERROR) {

View File

@ -6,12 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Element, LexerRange, Node, XmlParser} from '@angular/compiler';
import {I18nError} from './i18n_error';
import {TranslationParseError} from './translation_parse_error';
export function getAttrOrThrow(element: Element, attrName: string): string {
const attrValue = getAttribute(element, attrName);
if (attrValue === undefined) {
throw new I18nError(element.sourceSpan, `Missing required "${attrName}" attribute:`);
throw new TranslationParseError(
element.sourceSpan, `Missing required "${attrName}" attribute:`);
}
return attrValue;
}

View File

@ -8,7 +8,7 @@
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
import {MessageRenderer} from '../../../message_renderers/message_renderer';
import {BaseVisitor} from '../base_visitor';
import {I18nError} from '../i18n_error';
import {TranslationParseError} from '../translation_parse_error';
import {getAttrOrThrow} from '../translation_utils';
const INLINE_ELEMENTS = ['g', 'bx', 'ex', 'bpt', 'ept', 'ph', 'it', 'mrk'];
@ -29,7 +29,7 @@ export class Xliff1MessageSerializer<T> extends BaseVisitor {
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
visitAll(this, element.children);
} else {
throw new I18nError(element.sourceSpan, `Invalid element found in message.`);
throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
}
}

View File

@ -11,7 +11,7 @@ import {extname} from 'path';
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
import {TranslationBundle} from '../../../translator';
import {BaseVisitor} from '../base_visitor';
import {I18nError} from '../i18n_error';
import {TranslationParseError} from '../translation_parse_error';
import {TranslationParser} from '../translation_parser';
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
import {Xliff1MessageSerializer} from './xliff1_message_serializer';
@ -75,12 +75,13 @@ class XliffTranslationVisitor extends BaseVisitor {
if (element.name === 'trans-unit') {
const id = getAttrOrThrow(element, 'id');
if (this.translations[id] !== undefined) {
throw new I18nError(element.sourceSpan, `Duplicated translations for message "${id}"`);
throw new TranslationParseError(
element.sourceSpan, `Duplicated translations for message "${id}"`);
}
const targetMessage = element.children.find(isTargetElement);
if (targetMessage === undefined) {
throw new I18nError(element.sourceSpan, 'Missing required <target> element');
throw new TranslationParseError(element.sourceSpan, 'Missing required <target> element');
}
this.translations[id] = serializeTargetMessage(targetMessage);
} else {

View File

@ -8,7 +8,7 @@
import {Element, Expansion, ExpansionCase, Node, Text, visitAll} from '@angular/compiler';
import {MessageRenderer} from '../../../message_renderers/message_renderer';
import {BaseVisitor} from '../base_visitor';
import {I18nError} from '../i18n_error';
import {TranslationParseError} from '../translation_parse_error';
import {getAttrOrThrow, getAttribute} from '../translation_utils';
const INLINE_ELEMENTS = ['cp', 'sc', 'ec', 'mrk', 'sm', 'em'];
@ -33,7 +33,7 @@ export class Xliff2MessageSerializer<T> extends BaseVisitor {
} else if (INLINE_ELEMENTS.indexOf(element.name) !== -1) {
visitAll(this, element.children);
} else {
throw new I18nError(element.sourceSpan, `Invalid element found in message.`);
throw new TranslationParseError(element.sourceSpan, `Invalid element found in message.`);
}
}

View File

@ -11,7 +11,7 @@ import {extname} from 'path';
import {TargetMessageRenderer} from '../../../message_renderers/target_message_renderer';
import {TranslationBundle} from '../../../translator';
import {BaseVisitor} from '../base_visitor';
import {I18nError} from '../i18n_error';
import {TranslationParseError} from '../translation_parse_error';
import {TranslationParser} from '../translation_parser';
import {getAttrOrThrow, parseInnerRange} from '../translation_utils';
import {Xliff2MessageSerializer} from './xliff2_message_serializer';
@ -78,7 +78,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
if (element.name === 'unit') {
const externalId = getAttrOrThrow(element, 'id');
if (this.translations[externalId] !== undefined) {
throw new I18nError(
throw new TranslationParseError(
element.sourceSpan, `Duplicated translations for message "${externalId}"`);
}
visitAll(this, element.children, {unit: externalId});
@ -86,7 +86,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
assertTranslationUnit(element, context);
const targetMessage = element.children.find(isTargetElement);
if (targetMessage === undefined) {
throw new I18nError(element.sourceSpan, 'Missing required <target> element');
throw new TranslationParseError(element.sourceSpan, 'Missing required <target> element');
}
this.translations[context.unit] = serializeTargetMessage(targetMessage);
} else {
@ -97,7 +97,7 @@ class Xliff2TranslationVisitor extends BaseVisitor {
function assertTranslationUnit(segment: Element, context: any) {
if (context === undefined || context.unit === undefined) {
throw new I18nError(
throw new TranslationParseError(
segment.sourceSpan, 'Invalid <segment> element: should be a child of a <unit> element.');
}
}