Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(error): adding type to error in string validator in introspect #773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 2 additions & 1 deletion packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-core/lib/introspect/stringvalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

'use strict';

const { ErrorCodes } = require('@accordproject/concerto-util');
const { isNull } = require('../util');
const Validator = require('./validator');

Expand Down Expand Up @@ -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);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions packages/concerto-core/lib/introspect/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have used error codes as constants instead of inline strings for consistency

throw new BaseException('Validator error for field `' + id + '`. ' + this.getFieldOrScalarDeclaration().getFullyQualifiedName() + ': ' + msg, undefined, errorType);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/concerto-core/lib/typenotfoundexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const { BaseException } = require('@accordproject/concerto-util');
const { BaseException, ErrorCodes } = require('@accordproject/concerto-util');
const Globalize = require('./globalize');

/**
Expand All @@ -31,16 +31,17 @@ 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({
typeName: typeName
});
}

super(message, component);
super(message, component, errorType);
this.typeName = typeName;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-core/types/lib/introspect/validator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 2 additions & 1 deletion packages/concerto-core/types/lib/typenotfoundexception.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the main I still see constructor on TypeNotFoundException.js but why is this getting deleted here? Is this auto generated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is actually auto generated after I run build command

constructor(typeName: string, message: string | undefined, component: string, errorType?: string);
typeName: string;
/**
* Get the name of the type that was not found.
Expand Down
6 changes: 5 additions & 1 deletion packages/concerto-util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -67,5 +70,6 @@ module.exports = {
Logger,
TypedStack,
Label,
Identifiers
Identifiers,
ErrorCodes
};
7 changes: 6 additions & 1 deletion packages/concerto-util/lib/baseexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

'use strict';



const packageJson = require('../package.json');
const ErrorCodes = require('./errorcodes');

/**
* A base class for all Concerto exceptions
Expand All @@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a errortype parameter which is optional and provides the errorcode related to the error

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);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/concerto-util/lib/errorcodes.js
Original file line number Diff line number Diff line change
@@ -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';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error codes are defined in this file which can be used to implement different errors when ever needed

//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};
3 changes: 2 additions & 1 deletion packages/concerto-util/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
4 changes: 3 additions & 1 deletion packages/concerto-util/types/lib/baseexception.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 4 additions & 0 deletions packages/concerto-util/types/lib/errorcodes.d.ts
Original file line number Diff line number Diff line change
@@ -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";
Loading