diff --git a/packages/concerto-core/lib/introspect/illegalmodelexception.js b/packages/concerto-core/lib/introspect/illegalmodelexception.js index a4a72fea6..49b6da2d3 100644 --- a/packages/concerto-core/lib/introspect/illegalmodelexception.js +++ b/packages/concerto-core/lib/introspect/illegalmodelexception.js @@ -14,7 +14,7 @@ 'use strict'; -const { BaseFileException } = require('@accordproject/concerto-util'); +const {BaseFileException,ErrorCodes} = require('@accordproject/concerto-util'); // Types needed for TypeScript generation. /* eslint-disable no-unused-vars */ @@ -42,8 +42,9 @@ class IllegalModelException extends BaseFileException { * @param {number} fileLocation.end.line - end line of the error location. * @param {number} fileLocation.end.column - end column of the error location. * @param {string} [component] - the component which throws this error + * @param {string} [errorType='IllegalModelException'] - the error code */ - constructor(message, modelFile, fileLocation, component) { + constructor(message, modelFile, fileLocation, component, errorType = ErrorCodes.ILLEGAL_MODEL_EXCEPTION) { let messageSuffix = ''; let fileName = null; @@ -61,7 +62,7 @@ class IllegalModelException extends BaseFileException { // First character to be uppercase messageSuffix = messageSuffix.charAt(0).toUpperCase() + messageSuffix.slice(1); - super(message, fileLocation, message + ' ' + messageSuffix, fileName, component); + super(message, fileLocation, message + ' ' + messageSuffix, fileName, component, errorType); } } diff --git a/packages/concerto-core/lib/introspect/property.js b/packages/concerto-core/lib/introspect/property.js index db041f0d0..eb26e2ea8 100644 --- a/packages/concerto-core/lib/introspect/property.js +++ b/packages/concerto-core/lib/introspect/property.js @@ -18,6 +18,7 @@ const { MetaModelNamespace } = require('@accordproject/concerto-metamodel'); const ModelUtil = require('../modelutil'); const IllegalModelException = require('./illegalmodelexception'); +const TypeNotFoundException = require("../../lib/typenotfoundexception"); const Decorated = require('./decorated'); // Types needed for TypeScript generation. @@ -178,15 +179,15 @@ class Property extends Decorated { const parent = this.getParent(); if(!parent) { - throw new Error('Property ' + this.name + ' does not have a parent.'); + throw new IllegalModelException('Property ' + this.name + ' does not have a parent.'); } const modelFile = parent.getModelFile(); if(!modelFile) { - throw new Error('Parent of property ' + this.name + ' does not have a ModelFile!'); + throw new IllegalModelException('Parent of property ' + this.name + ' does not have a ModelFile!'); } const result = modelFile.getFullyQualifiedTypeName(this.type); if(!result) { - throw new Error('Failed to find fully qualified type name for property ' + this.name + ' with type ' + this.type ); + throw new TypeNotFoundException(this.type,'Failed to find fully qualified type name for property ' + this.name + ' with type ' + this.type); } return result; diff --git a/packages/concerto-core/test/introspect/property_ex.js b/packages/concerto-core/test/introspect/property_ex.js index b04e6ffe8..d7ae4a054 100644 --- a/packages/concerto-core/test/introspect/property_ex.js +++ b/packages/concerto-core/test/introspect/property_ex.js @@ -15,6 +15,8 @@ 'use strict'; const ModelManager = require('../../lib/modelmanager'); +const IllegalModelException = require('../../lib/introspect/illegalmodelexception'); +const TypeNotFoundException = require('../../lib/typenotfoundexception'); const sinon = require('sinon'); const Util = require('../composer/composermodelutility'); @@ -56,19 +58,27 @@ describe('Property', function () { // stub the getType method to return null sinon.stub(field, 'getParent').callsFake(function(){return null;}); - (function () { + try{ field.getFullyQualifiedTypeName(); - }).should.throw(/Property owner does not have a parent./); + }catch(err){ + err.errorType.should.match(/IllegalModelException/); + err.should.be.an.instanceOf(IllegalModelException); + err.message.should.match(/Property owner does not have a parent./); + } }); it('should throw if parent has no ModelFile', function () { const person = modelManager.getType('org.acme.l1.Car'); const field = person.getProperty('owner'); // stub the getType method to return null sinon.stub(person, 'getModelFile').callsFake(function(){return null;}); - - (function () { + + try{ field.getFullyQualifiedTypeName(); - }).should.throw(/Parent of property owner does not have a ModelFile!/); + }catch(err){ + err.errorType.should.match(/IllegalModelException/); + err.should.be.an.instanceOf(IllegalModelException); + err.message.should.match(/Parent of property owner does not have a ModelFile!/); + } }); it('should throw if ModelFile fails to find type', function () { const person = modelManager.getType('org.acme.l1.Car'); @@ -76,9 +86,13 @@ describe('Property', function () { // stub the getType method to return null sinon.stub(person.getModelFile(), 'getFullyQualifiedTypeName').callsFake(function(){return null;}); - (function () { + try{ field.getFullyQualifiedTypeName(); - }).should.throw(/Failed to find fully qualified type name for property owner with type Person/); + }catch(err){ + err.errorType.should.match(/TypeNotFoundException/); + err.should.be.an.instanceOf(TypeNotFoundException); + err.message.should.match(/Failed to find fully qualified type name for property owner with type Person/); + } }); it('toString works', function () { const person = modelManager.getType('org.acme.l1.Car'); diff --git a/packages/concerto-core/types/lib/introspect/illegalmodelexception.d.ts b/packages/concerto-core/types/lib/introspect/illegalmodelexception.d.ts index 566557f32..ffcd8b01c 100644 --- a/packages/concerto-core/types/lib/introspect/illegalmodelexception.d.ts +++ b/packages/concerto-core/types/lib/introspect/illegalmodelexception.d.ts @@ -17,8 +17,9 @@ declare class IllegalModelException extends BaseFileException { * @param {number} fileLocation.end.line - end line of the error location. * @param {number} fileLocation.end.column - end column of the error location. * @param {string} [component] - the component which throws this error + * @param {string} [errorType='IllegalModelException'] - the error code */ - constructor(message: string, modelFile?: ModelFile, fileLocation?: any, component?: string); + constructor(message: string, modelFile?: ModelFile, fileLocation?: any, component?: string, errorType?: string); } import { BaseFileException } from "@accordproject/concerto-util"; import ModelFile = require("./modelfile"); diff --git a/packages/concerto-util/lib/basefileexception.js b/packages/concerto-util/lib/basefileexception.js index 994dc7c11..df1e85f03 100644 --- a/packages/concerto-util/lib/basefileexception.js +++ b/packages/concerto-util/lib/basefileexception.js @@ -15,6 +15,7 @@ 'use strict'; const BaseException = require('./baseexception'); +const { BASE_FILE_EXCEPTION } = require("./errorcodes"); /** * Exception throws when a Concerto file is semantically invalid @@ -31,9 +32,10 @@ class BaseFileException extends BaseException { * @param {string} fullMessage - the optional full message text * @param {string} [fileName] - the file name * @param {string} [component] - the component which throws this error + * @param {string} [errorType='BaseFileException'] - the error code */ - constructor(message, fileLocation, fullMessage, fileName, component) { - super(fullMessage ? fullMessage : message, component); + constructor(message, fileLocation, fullMessage, fileName, component, errorType = BASE_FILE_EXCEPTION) { + super(fullMessage ? fullMessage : message, component, errorType); this.fileLocation = fileLocation; this.shortMessage = message; this.fileName = fileName; diff --git a/packages/concerto-util/lib/errorcodes.js b/packages/concerto-util/lib/errorcodes.js index 3f6ec0628..0a5080328 100644 --- a/packages/concerto-util/lib/errorcodes.js +++ b/packages/concerto-util/lib/errorcodes.js @@ -22,5 +22,9 @@ const DEFAULT_VALIDATOR_EXCEPTION = 'DefaultValidatorException'; const REGEX_VALIDATOR_EXCEPTION = 'RegexValidatorException'; // base exception for Type not found const TYPE_NOT_FOUND_EXCEPTION = 'TypeNotFoundException'; +// base exception for illegal model +const ILLEGAL_MODEL_EXCEPTION = 'IllegalModelException'; +// base file exception +const BASE_FILE_EXCEPTION = "BaseFileException"; -module.exports = {DEFAULT_BASE_EXCEPTION, DEFAULT_VALIDATOR_EXCEPTION, REGEX_VALIDATOR_EXCEPTION, TYPE_NOT_FOUND_EXCEPTION}; \ No newline at end of file +module.exports = {DEFAULT_BASE_EXCEPTION, DEFAULT_VALIDATOR_EXCEPTION, REGEX_VALIDATOR_EXCEPTION, TYPE_NOT_FOUND_EXCEPTION,ILLEGAL_MODEL_EXCEPTION,BASE_FILE_EXCEPTION}; \ No newline at end of file diff --git a/packages/concerto-util/types/lib/basefileexception.d.ts b/packages/concerto-util/types/lib/basefileexception.d.ts index e0381effc..d62e0cef0 100644 --- a/packages/concerto-util/types/lib/basefileexception.d.ts +++ b/packages/concerto-util/types/lib/basefileexception.d.ts @@ -14,8 +14,9 @@ declare class BaseFileException extends BaseException { * @param {string} fullMessage - the optional full message text * @param {string} [fileName] - the file name * @param {string} [component] - the component which throws this error + * @param {string} [errorType='BaseFileException'] - the error code */ - constructor(message: string, fileLocation: string, fullMessage: string, fileName?: string, component?: string); + constructor(message: string, fileLocation: string, fullMessage: string, fileName?: string, component?: string, errorType?: string); fileLocation: string; shortMessage: string; fileName: string; diff --git a/packages/concerto-util/types/lib/errorcodes.d.ts b/packages/concerto-util/types/lib/errorcodes.d.ts index 5bdae0591..039204e46 100644 --- a/packages/concerto-util/types/lib/errorcodes.d.ts +++ b/packages/concerto-util/types/lib/errorcodes.d.ts @@ -2,3 +2,5 @@ export const DEFAULT_BASE_EXCEPTION: "DefaultBaseException"; export const DEFAULT_VALIDATOR_EXCEPTION: "DefaultValidatorException"; export const REGEX_VALIDATOR_EXCEPTION: "RegexValidatorException"; export const TYPE_NOT_FOUND_EXCEPTION: "TypeNotFoundException"; +export const ILLEGAL_MODEL_EXCEPTION: "IllegalModelException"; +export const BASE_FILE_EXCEPTION: "BaseFileException";