Skip to content

Commit

Permalink
refactor: update modelfile to use globalize for error formatting
Browse files Browse the repository at this point in the history
Signed-off-by: AbhinRustagi <[email protected]>
  • Loading branch information
AbhinRustagi committed Oct 18, 2024
1 parent 87aa28e commit c7f944a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/concerto-core/lib/introspect/modelfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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
}));
}
}
}
Expand All @@ -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
}));
}
}
}
Expand All @@ -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);
}
});

Expand All @@ -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.',
Expand All @@ -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);
});
Expand All @@ -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}`);
Expand Down
11 changes: 11 additions & 0 deletions packages/concerto-core/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}\".",

Expand Down

0 comments on commit c7f944a

Please sign in to comment.