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

Conversation

santanu8961
Copy link
Contributor

@santanu8961 santanu8961 commented Dec 15, 2023

Closes #772

Changes

Adds type to validator error in introspect validator.

  • added a new optional parameter to class BaseException which can take errorType as well as errorMessage which helps to determine what kind of error is getting thrown from the validator itself. and using that to throw error in validator.
  • also added this in NotFoundException for better complience

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
@sanketshevkar
Copy link
Member

Should we have a new folder for error classes and not keep it in introspect?

* @class
* @memberof module:concerto-core
*/
class StringValidationException extends Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this can be named just ValidationException. If not, name of the class should be the same as the file name.

File name is stringvalidaTORexception and name of the class is StringValidaTIONException.

Copy link
Member

Choose a reason for hiding this comment

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

It should be Validator, not Validation. Internally the regex keyword is called a StringValidator.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we should ensure we can use similar approach with NumberValidator as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @mttrbrts , @ekarademir I am keeping the name of the class as StringValidatiorException and the default error as ValidatorError and the regex error as RegexValidatorException.
let me know if this works fine . and this is what you meant.

* @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='ValidatorError') {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the use of this additional argument? How is it different than checking the type of the error that's been thrown?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's avoid having to use instanceof checks - those make assumptions about how packages are required (instanceof can fail if the same code is loaded at different version) which have bitten us in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @ekarademir I have used the additional argument errorType so that it can be propagated through till StringValidationException where we can use it to denote what kind of error it is , which can be very useful to do any kind of operation as only error string is quick tricky to detect what error it is trying to throw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @dselman , can you please clarify what do you mean by the instance of checks?
Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean client code doing things like:

try {
...
}
catch(err) {
if(err instanceof StringValidatorException) {
 ...
}
}

Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

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

I like the motivation for this. I just needs a bit of refactoring to ensure it works with NumberValidator and can be cleanly consumed upstream.

* @class
* @memberof module:concerto-core
*/
class StringValidationException extends Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we should ensure we can use similar approach with NumberValidator as well.

* @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='ValidatorError') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's avoid having to use instanceof checks - those make assumptions about how packages are required (instanceof can fail if the same code is loaded at different version) which have bitten us in the past.

Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

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

I would prefer to see generic Error Code functionality moved into concerto-util/BaseException, and to refactor code to throw Errors that derive from BaseException.

Error codes should be defined constants so that code is type safe.

* @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='ValidatorError') {
Copy link
Contributor

Choose a reason for hiding this comment

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

I mean client code doing things like:

try {
...
}
catch(err) {
if(err instanceof StringValidatorException) {
 ...
}
}

@@ -70,7 +70,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,'RegexValidatorException');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we want to go this route then the error codes should be defined and exported and referenced.

E.g. something like:

ErrorCodes.js
export const INVALID_REGEX = 1;

Somewhere we want to report an error:

this.reportError(field.getName(), exception.message, ErrorCodes.INVALID_REGEX);

This allows a client to do something like:

import {ErrorCodes} from '@accordproject/concerto-core';

try {
...
}
catch(err) {
const errorCode = err?.getErrorCode.?();
switch(errorCode) {
case ErrorCodes.INVALID_REGEX:
  // do something specific here
break;
default:
  // general case
}

We should have in mind that concerto-core will be moving to TypeScript and we want these error codes to be type safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @dselman , as you requested I have changed the structure by doing the following changes to the structure:

  1. I no longer am using the StringValidatorException class instead I have added the errorType as an optional param in BaseException and using that.
  2. added an errorCodes.js file where I am storing the error type constants and using them directly , these are exposed so the client will also be able to use these , as discussed in the PR

* @class
* @memberof module:concerto-core
*/
class StringValidatorException extends Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be more generic - it extends Error and adds an Error Code. We could add an optional error code to concerto-util/BaseException, so that all exceptions can reference an error code.

@santanu8961 santanu8961 force-pushed the santanur/i772/provide-proper-error-object-instead-of-message-for-string-error-introspect branch from a3549e8 to 846fff2 Compare December 28, 2023 10:03
@@ -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);
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

* @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

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 ,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.

removed StringValidatorExceptionClass as its not needed anymore I have added a optional param errorType in BaseException which solves the purpose

*/

'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

@santanu8961 santanu8961 force-pushed the santanur/i772/provide-proper-error-object-instead-of-message-for-string-error-introspect branch from 846fff2 to 1139dce Compare December 28, 2023 11:46
*/
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

@santanu8961
Copy link
Contributor Author

santanu8961 commented Dec 28, 2023

Hi @dselman , as you requested I have changed the structure by doing the following changes to the structure:

  1. I no longer am using the StringValidatorException class instead I have added the errorType as an optional param in BaseException and using that.
  2. added an errorCodes.js file where I am storing the error type constants and using them directly , these are exposed so the client will also be able to use these , as discussed in the PR .

Santanu Roy added 2 commits December 28, 2023 23:46
Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Signed-off-by: Santanu Roy <santanu.roy@docusign.com>
Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Signed-off-by: Santanu Roy <santanu.roy@docusign.com>
@santanu8961 santanu8961 force-pushed the santanur/i772/provide-proper-error-object-instead-of-message-for-string-error-introspect branch from 1139dce to 6fce3db Compare December 28, 2023 18:18
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

nit: lint check in pipeline could throw error here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved

* @param {string|undefined} message - error message.
* @param {string} component - the optional component which throws this 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

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);
Copy link
Member

Choose a reason for hiding this comment

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

nit: Space linting, same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

resolved

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Copy link
Contributor

@dselman dselman left a comment

Choose a reason for hiding this comment

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

Good work. Thanks!

@dselman dselman enabled auto-merge (squash) January 2, 2024 14:16
@dselman dselman merged commit 769baa0 into accordproject:main Jan 2, 2024
11 checks passed
hereje pushed a commit to hereje/concerto that referenced this pull request Feb 29, 2024
…ccordproject#773)

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

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Co-authored-by: Santanu Roy <santanu.roy@docusign.com>
Signed-off-by: Angel Mendez Cano <amendez1988@gmail.com>
mttrbrts pushed a commit to mttrbrts/composer-concerto that referenced this pull request Mar 10, 2024
…ccordproject#773)

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

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Co-authored-by: Santanu Roy <santanu.roy@docusign.com>
mttrbrts pushed a commit to mttrbrts/composer-concerto that referenced this pull request Mar 11, 2024
…ccordproject#773)

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

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Co-authored-by: Santanu Roy <santanu.roy@docusign.com>
mttrbrts added a commit that referenced this pull request Mar 11, 2024
* fix(build): include unions in index

Signed-off-by: Matt Roberts <code@rbrts.uk>

* chore(deps): upgrade codegen to latest release

Signed-off-by: Matt Roberts <code@rbrts.uk>

---------

Signed-off-by: Matt Roberts <code@rbrts.uk>

* fix(class): throw error when class is extending itself (#767)

* fix(parser): throw error when concept is extending itself in CTO

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* fix(parser): throw error when concept is extending itself in JSON metamodel form

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* fix(parser): throw error when concept is extending itself in the AST

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* refactor(validation): alphabetical rearrangement

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* test(self-extending): remove redundant tests (codepath covered in concerto-cto)

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* test(fix): remove unneeded import

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

---------

Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>
Co-authored-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>

* fix(class-declaration): throw with undefined ast properties (#771)

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

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

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

Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Co-authored-by: Santanu Roy <santanu.roy@docusign.com>

* refactor(declarations): move declaration uniqueness check to model file (#794)

* refactor(declarations): Move unique name check to model file.

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor(test): move validation checks for duplicate class to model file

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* test: empty commit to trigger test

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

---------

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* perf(core): don't use arrays to check uniqueness (#802)

refactor: don't use arrays to check uniqueness

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* perf(core): remove usage of arrays while forming duplicate item errors (#804)

* refactor: don't use arrays to check uniqueness

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: also refactor unique property name check

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: remove array for decorator uniqueness check

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

* refactor: remove uniqueness check from scalar declarations as well

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

---------

Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>

---------

Signed-off-by: Matt Roberts <code@rbrts.uk>
Signed-off-by: Stefan Blaginov <stefanblaginov@gmail.com>
Signed-off-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>
Signed-off-by: Ertugrul Karademir <ekarademir@gmail.com>
Signed-off-by: Santanu Roy <edu.santanu12@gmail.com>
Co-authored-by: Stefan Blaginov <stefanblaginov@gmail.com>
Co-authored-by: Stefan Blaginov <Stefan.Blaginov@docusign.com>
Co-authored-by: Ertugrul Karademir <ekarademir@gmail.com>
Co-authored-by: Santanu Roy <43335319+santanu8961@users.noreply.github.com>
Co-authored-by: Santanu Roy <santanu.roy@docusign.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants