From c7f944a8a1ebeb565e25f65fd4d43f04f9604e01 Mon Sep 17 00:00:00 2001 From: AbhinRustagi Date: Fri, 18 Oct 2024 12:09:17 +1100 Subject: [PATCH] refactor: update modelfile to use globalize for error formatting Signed-off-by: AbhinRustagi --- .../concerto-core/lib/introspect/modelfile.js | 30 ++++++++++++------- packages/concerto-core/messages/en.json | 11 +++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/concerto-core/lib/introspect/modelfile.js b/packages/concerto-core/lib/introspect/modelfile.js index 078926bfa..d45100364 100644 --- a/packages/concerto-core/lib/introspect/modelfile.js +++ b/packages/concerto-core/lib/introspect/modelfile.js @@ -76,18 +76,18 @@ class ModelFile extends Decorated { this.version = null; if(!ast || typeof ast !== 'object') { - throw new Error('ModelFile expects a Concerto model AST as input.'); + throw new Error(Globalize.formatMessage('modelfile-constructor-astnotobject')); } this.ast = ast; if(definitions && typeof definitions !== 'string') { - throw new Error('ModelFile expects an (optional) Concerto model definition as a string.'); + throw new Error(Globalize.formatMessage('modelfile-constructor-defnotstring')); } this.definitions = definitions; if(fileName && typeof fileName !== 'string') { - throw new Error('ModelFile expects an (optional) filename as a string.'); + throw new Error(Globalize.formatMessage('modelfile-constructor-filenamenotstring')); } this.fileName = fileName; @@ -482,7 +482,7 @@ class ModelFile extends Decorated { */ getLocalType(type) { if(!this.localTypes) { - throw new Error('Internal error: local types are not yet initialized. Do not try to resolve types inside `process`.'); + throw new Error(Globalize.formatMessage('modelfile-getlocaltype-notinit')); } if(!type.startsWith(this.getNamespace())) { @@ -698,7 +698,11 @@ class ModelFile extends Decorated { if (semver.satisfies(packageJson.version, this.ast.concertoVersion, { includePrerelease: true })) { this.concertoVersion = this.ast.concertoVersion; } else { - throw new Error(`ModelFile expects Concerto version ${this.ast.concertoVersion} but this is ${packageJson.version}`); + let formatter = Globalize.messageFormatter('modelfile-incompatibleversion'); + throw new Error(formatter({ + concertoVersion: this.ast.concertoVersion, + packageJsonVersion: packageJson.version + })); } } } @@ -712,7 +716,10 @@ class ModelFile extends Decorated { if(this.getModelManager().isStrict()) { const nsInfo = ModelUtil.parseNamespace(imp.namespace); if(!nsInfo.version) { - throw new Error(`Cannot use an unversioned import ${imp.namespace} when 'strict' option on Model Manager is set.`); + let formatter = Globalize.messageFormatter('modelfile-unversionedimport'); + throw new Error(formatter({ + namespace: imp.namespace + })); } } } @@ -728,7 +735,10 @@ class ModelFile extends Decorated { const namespaceParts = nsInfo.name.split('.'); namespaceParts.forEach(part => { if (!ModelUtil.isValidIdentifier(part)){ - throw new IllegalModelException(`Invalid namespace part '${part}'`, this.modelFile, this.ast.location); + let formatter = Globalize.messageFormatter('modelfile-invalidnamespacepart'); + throw new IllegalModelException(formatter({ + part: part + }), this.modelFile, this.ast.location); } }); @@ -754,7 +764,7 @@ class ModelFile extends Decorated { switch(imp.$class) { case `${MetaModelNamespace}.ImportAll`: if (this.getModelManager().isStrict()){ - throw new Error('Wilcard Imports are not permitted in strict mode.'); + throw new Error(Globalize.formatMessage('modelfile-wildcardimport-notallowed')); } Warning.printDeprecationWarning( 'Wilcard Imports are deprecated in this version of Concerto and will be removed in a future version.', @@ -770,7 +780,7 @@ class ModelFile extends Decorated { if (imp.aliasedTypes) { imp.aliasedTypes.forEach(({ name, aliasedName }) => { if(ModelUtil.isPrimitiveType(aliasedName)){ - throw new Error('Types cannot be aliased to primitive type'); + throw new Error(Globalize.formatMessage('modelfile-aliastype-primitive')); } aliasedTypes.set(name, aliasedName); }); @@ -789,7 +799,7 @@ class ModelFile extends Decorated { ); } else { if (imp.aliasedTypes) { - throw new Error('Aliasing disabled, set importAliasing to true'); + throw new Error(Globalize.formatMessage('modelfile-aliastype-disabled')); } imp.types.forEach((type) => { this.importShortNames.set(type,`${imp.namespace}.${type}`); diff --git a/packages/concerto-core/messages/en.json b/packages/concerto-core/messages/en.json index 4fc86fbe5..f3357e5d7 100644 --- a/packages/concerto-core/messages/en.json +++ b/packages/concerto-core/messages/en.json @@ -69,6 +69,17 @@ "instancegenerator-newinstance-noconcreteclass": "No concrete extending type for \"{type}\".", + "modelfile-constructor-astnotobject": "ModelFile expects a Concerto model AST as input.", + "modelfile-constructor-defnotstring": "ModelFile expects an (optional) Concerto model definition as a string.", + "modelfile-constructor-filenamenotstring": "ModelFile expects an (optional) filename as a string.", + "modelfile-getlocaltype-notinit": "Internal error: local types are not yet initialized. Do not try to resolve types inside `process`.", + "modelfile-incompatibleversion": "ModelFile expects Concerto version \"{concertoVersion}\" but this is \"{packageJsonVersion}\"", + "modelfile-unversionedimport": "Cannot use an unversioned import \"{namespace}\" when 'strict' option on Model Manager is set.", + "modelfile-invalidnamespacepart": "Invalid namespace part '\"{part}\"'", + "modelfile-wildcardimport-notallowed": "Wilcard Imports are not permitted in strict mode.", + "modelfile-aliastype-primitive": "Types cannot be aliased to primitive type", + "modelfile-aliastype-disabled": "Aliasing disabled, set importAliasing to true", + "modelmanager-resolvetype-nonsfortype": "No registered namespace for type \"{type}\" in \"{context}\".", "modelmanager-resolvetype-notypeinnsforcontext": "No type \"{type}\" in namespace \"{namespace}\" for \"{context}\".",