diff --git a/packages/concerto-core/api.txt b/packages/concerto-core/api.txt index 7098113628..ae8ed8fa7c 100644 --- a/packages/concerto-core/api.txt +++ b/packages/concerto-core/api.txt @@ -344,6 +344,6 @@ class Serializer { + Resource fromJSON(Object,Object?,boolean,boolean,number?) } class TypeNotFoundException extends BaseException { - + void constructor(string,string|,string) + + void constructor(string,string|,string,string) + string getTypeName() } diff --git a/packages/concerto-core/changelog.txt b/packages/concerto-core/changelog.txt index c65e01c244..a1e87d9a4a 100644 --- a/packages/concerto-core/changelog.txt +++ b/packages/concerto-core/changelog.txt @@ -24,8 +24,9 @@ # Note that the latest public API is documented using JSDocs and is available in api.txt. # -Version 3.13.3 {b286dfdeeb654d25be7c5f9cc6305e38} 2023-11-07 +Version 3.13.3 {8f59b43e6071c4d3ae42e94476142f7a} 2023-11-07 - Added DCS and vocabulary extraction support for decoratorManager +- Added errortype to BaseException and used that to define error types in introspect Version 3.13.2 {dccc690753912cf87e7ceec56d949058} 2023-10-18 - Add getNamespace method to key type and value type of maps diff --git a/packages/concerto-core/lib/introspect/stringvalidator.js b/packages/concerto-core/lib/introspect/stringvalidator.js index 290b725ca0..b3c6db8a39 100644 --- a/packages/concerto-core/lib/introspect/stringvalidator.js +++ b/packages/concerto-core/lib/introspect/stringvalidator.js @@ -14,6 +14,7 @@ 'use strict'; +const { ErrorCodes } = require('@accordproject/concerto-util'); const { isNull } = require('../util'); const Validator = require('./validator'); @@ -70,7 +71,7 @@ class StringValidator extends Validator{ this.regex = new CustomRegExp(validator.pattern, validator.flags); } catch(exception) { - this.reportError(field.getName(), exception.message); + this.reportError(field.getName(), exception.message, ErrorCodes.REGEX_VALIDATOR_EXCEPTION); } } } diff --git a/packages/concerto-core/lib/introspect/validator.js b/packages/concerto-core/lib/introspect/validator.js index 9bf0b0ac07..3f790e6f36 100644 --- a/packages/concerto-core/lib/introspect/validator.js +++ b/packages/concerto-core/lib/introspect/validator.js @@ -14,6 +14,7 @@ 'use strict'; +const { BaseException, ErrorCodes } = require('@accordproject/concerto-util'); // Types needed for TypeScript generation. /* eslint-disable no-unused-vars */ /* istanbul ignore next */ @@ -46,10 +47,11 @@ class Validator { /** * @param {string} id the identifier of the instance * @param {string} msg the exception message + * @param {string} errorType the type of error * @throws {Error} throws an error to report the message */ - reportError(id, msg) { - throw new Error( 'Validator error for field `' + id + '`. ' + this.getFieldOrScalarDeclaration().getFullyQualifiedName() + ': ' + msg ); + reportError(id, msg, errorType=ErrorCodes.DEFAULT_VALIDATOR_EXCEPTION) { + throw new BaseException('Validator error for field `' + id + '`. ' + this.getFieldOrScalarDeclaration().getFullyQualifiedName() + ': ' + msg, undefined, errorType); } /** diff --git a/packages/concerto-core/lib/typenotfoundexception.js b/packages/concerto-core/lib/typenotfoundexception.js index d478182abd..29f21de097 100644 --- a/packages/concerto-core/lib/typenotfoundexception.js +++ b/packages/concerto-core/lib/typenotfoundexception.js @@ -14,7 +14,7 @@ 'use strict'; -const { BaseException } = require('@accordproject/concerto-util'); +const { BaseException, ErrorCodes } = require('@accordproject/concerto-util'); const Globalize = require('./globalize'); /** @@ -31,8 +31,9 @@ class TypeNotFoundException extends BaseException { * @param {string} typeName - fully qualified type name. * @param {string|undefined} message - error message. * @param {string} component - the optional component which throws this error + * @param {string} errorType - the error code related to the error */ - constructor(typeName, message, component) { + constructor(typeName, message, component, errorType = ErrorCodes.TYPE_NOT_FOUND_EXCEPTION) { if (!message) { const formatter = Globalize.messageFormatter('typenotfounderror-defaultmessage'); message = formatter({ @@ -40,7 +41,7 @@ class TypeNotFoundException extends BaseException { }); } - super(message, component); + super(message, component, errorType); this.typeName = typeName; } diff --git a/packages/concerto-core/types/lib/introspect/validator.d.ts b/packages/concerto-core/types/lib/introspect/validator.d.ts index 077536b5be..b7627d9da1 100644 --- a/packages/concerto-core/types/lib/introspect/validator.d.ts +++ b/packages/concerto-core/types/lib/introspect/validator.d.ts @@ -20,9 +20,10 @@ declare class Validator { /** * @param {string} id the identifier of the instance * @param {string} msg the exception message + * @param {string} errorType the type of error * @throws {Error} throws an error to report the message */ - reportError(id: string, msg: string): void; + reportError(id: string, msg: string, errorType?: string): void; /** * Visitor design pattern * @param {Object} visitor - the visitor diff --git a/packages/concerto-core/types/lib/serializer/validationexception.d.ts b/packages/concerto-core/types/lib/serializer/validationexception.d.ts index ef5846bbe9..8978052036 100644 --- a/packages/concerto-core/types/lib/serializer/validationexception.d.ts +++ b/packages/concerto-core/types/lib/serializer/validationexception.d.ts @@ -8,6 +8,12 @@ export = ValidationException; * @private */ declare class ValidationException extends BaseException { + /** + * Create a ValidationException + * @param {string} message - the message for the exception + * @param {string} component - the optional component which throws this error + */ + constructor(message: string, component: string); } import BaseException_1 = require("@accordproject/concerto-util/types/lib/baseexception"); import BaseException = BaseException_1.BaseException; diff --git a/packages/concerto-core/types/lib/typenotfoundexception.d.ts b/packages/concerto-core/types/lib/typenotfoundexception.d.ts index 231c679315..b5af576b50 100644 --- a/packages/concerto-core/types/lib/typenotfoundexception.d.ts +++ b/packages/concerto-core/types/lib/typenotfoundexception.d.ts @@ -13,8 +13,9 @@ declare class TypeNotFoundException extends BaseException { * @param {string} typeName - fully qualified type name. * @param {string|undefined} message - error message. * @param {string} component - the optional component which throws this error + * @param {string} errorType - the error code related to the error */ - constructor(typeName: string, message: string | undefined, component: string); + constructor(typeName: string, message: string | undefined, component: string, errorType?: string); typeName: string; /** * Get the name of the type that was not found. diff --git a/packages/concerto-util/index.js b/packages/concerto-util/index.js index 02fb6192f9..65862da031 100644 --- a/packages/concerto-util/index.js +++ b/packages/concerto-util/index.js @@ -52,6 +52,9 @@ const Label = require('./lib/label'); // Identifiers const Identifiers = require('./lib/identifiers'); +//errorcodes +const ErrorCodes = require('./lib/errorcodes'); + module.exports = { BaseException, BaseFileException, @@ -67,5 +70,6 @@ module.exports = { Logger, TypedStack, Label, - Identifiers + Identifiers, + ErrorCodes }; diff --git a/packages/concerto-util/lib/baseexception.js b/packages/concerto-util/lib/baseexception.js index 896f0759c8..bd967c1dad 100644 --- a/packages/concerto-util/lib/baseexception.js +++ b/packages/concerto-util/lib/baseexception.js @@ -14,7 +14,10 @@ 'use strict'; + + const packageJson = require('../package.json'); +const ErrorCodes = require('./errorcodes'); /** * A base class for all Concerto exceptions @@ -27,12 +30,14 @@ class BaseException extends Error { * Create the BaseException. * @param {string} message - The exception message. * @param {string} component - The optional component which throws this error. + * @param {string} errorType - The optional error code regarding the error */ - constructor(message, component) { + constructor(message, component, errorType) { super(message); this.component = component || packageJson.name; this.name = this.constructor.name; this.message = message; + this.errorType = errorType || ErrorCodes.DEFAULT_BASE_EXCEPTION; if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor); } diff --git a/packages/concerto-util/lib/errorcodes.js b/packages/concerto-util/lib/errorcodes.js new file mode 100644 index 0000000000..3f6ec0628b --- /dev/null +++ b/packages/concerto-util/lib/errorcodes.js @@ -0,0 +1,26 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +//default base exception +const DEFAULT_BASE_EXCEPTION = 'DefaultBaseException'; +//default validator exception which is being used when there is no specified validator exception in introspect +const DEFAULT_VALIDATOR_EXCEPTION = 'DefaultValidatorException'; +// exception code for regex validator format error +const REGEX_VALIDATOR_EXCEPTION = 'RegexValidatorException'; +// base exception for Type not found +const TYPE_NOT_FOUND_EXCEPTION = 'TypeNotFoundException'; + +module.exports = {DEFAULT_BASE_EXCEPTION, DEFAULT_VALIDATOR_EXCEPTION, REGEX_VALIDATOR_EXCEPTION, TYPE_NOT_FOUND_EXCEPTION}; \ No newline at end of file diff --git a/packages/concerto-util/types/index.d.ts b/packages/concerto-util/types/index.d.ts index 3d3d7a0248..e87904ac9a 100644 --- a/packages/concerto-util/types/index.d.ts +++ b/packages/concerto-util/types/index.d.ts @@ -13,4 +13,5 @@ import Logger = require("./lib/logger"); import TypedStack = require("./lib/typedstack"); import Label = require("./lib/label"); import Identifiers = require("./lib/identifiers"); -export { BaseException, BaseFileException, FileDownloader, CompositeFileLoader, DefaultFileLoader, GitHubFileLoader, HTTPFileLoader, Writer, FileWriter, InMemoryWriter, ModelWriter, Logger, TypedStack, Label, Identifiers }; +import ErrorCodes = require("./lib/errorcodes"); +export { BaseException, BaseFileException, FileDownloader, CompositeFileLoader, DefaultFileLoader, GitHubFileLoader, HTTPFileLoader, Writer, FileWriter, InMemoryWriter, ModelWriter, Logger, TypedStack, Label, Identifiers, ErrorCodes }; diff --git a/packages/concerto-util/types/lib/baseexception.d.ts b/packages/concerto-util/types/lib/baseexception.d.ts index bdc321df80..f6f5cadd8d 100644 --- a/packages/concerto-util/types/lib/baseexception.d.ts +++ b/packages/concerto-util/types/lib/baseexception.d.ts @@ -10,7 +10,9 @@ declare class BaseException extends Error { * Create the BaseException. * @param {string} message - The exception message. * @param {string} component - The optional component which throws this error. + * @param {string} errorType - The optional error code regarding the error */ - constructor(message: string, component: string); + constructor(message: string, component: string, errorType: string); component: any; + errorType: string; } diff --git a/packages/concerto-util/types/lib/errorcodes.d.ts b/packages/concerto-util/types/lib/errorcodes.d.ts new file mode 100644 index 0000000000..5bdae05915 --- /dev/null +++ b/packages/concerto-util/types/lib/errorcodes.d.ts @@ -0,0 +1,4 @@ +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";