-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Signed-off-by: Santanu Roy <[email protected]>
packages/concerto-core/lib/introspect/stringvalidatorexception.js
Outdated
Show resolved
Hide resolved
packages/concerto-core/lib/introspect/stringvalidatorexception.js
Outdated
Show resolved
Hide resolved
Should we have a new folder for error classes and not keep it in introspect? |
* @class | ||
* @memberof module:concerto-core | ||
*/ | ||
class StringValidationException extends Error { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) {
...
}
}
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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') { |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- I no longer am using the StringValidatorException class instead I have added the errorType as an optional param in BaseException and using that.
- 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 { |
There was a problem hiding this comment.
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.
a3549e8
to
846fff2
Compare
@@ -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); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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'; | ||
|
There was a problem hiding this comment.
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
846fff2
to
1139dce
Compare
*/ | ||
constructor(message, component) { | ||
constructor(message, component, errorType) { |
There was a problem hiding this comment.
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
Hi @dselman , as you requested I have changed the structure by doing the following changes to the structure:
|
Signed-off-by: Santanu Roy <[email protected]> Signed-off-by: Santanu Roy <[email protected]>
Signed-off-by: Santanu Roy <[email protected]> Signed-off-by: Santanu Roy <[email protected]>
1139dce
to
6fce3db
Compare
@@ -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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work. Thanks!
…ccordproject#773) * fix(error): adding type to error in string validator in introspect Signed-off-by: Santanu Roy <[email protected]> Co-authored-by: Santanu Roy <[email protected]> Signed-off-by: Angel Mendez Cano <[email protected]>
…ccordproject#773) * fix(error): adding type to error in string validator in introspect Signed-off-by: Santanu Roy <[email protected]> Co-authored-by: Santanu Roy <[email protected]>
…ccordproject#773) * fix(error): adding type to error in string validator in introspect Signed-off-by: Santanu Roy <[email protected]> Co-authored-by: Santanu Roy <[email protected]>
* fix(build): include unions in index Signed-off-by: Matt Roberts <[email protected]> * chore(deps): upgrade codegen to latest release Signed-off-by: Matt Roberts <[email protected]> --------- Signed-off-by: Matt Roberts <[email protected]> * 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 <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> * fix(parser): throw error when concept is extending itself in JSON metamodel form Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> * fix(parser): throw error when concept is extending itself in the AST Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> * refactor(validation): alphabetical rearrangement Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> * test(self-extending): remove redundant tests (codepath covered in concerto-cto) Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> * test(fix): remove unneeded import Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> --------- Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> Co-authored-by: Stefan Blaginov <[email protected]> * fix(class-declaration): throw with undefined ast properties (#771) Signed-off-by: Ertugrul Karademir <[email protected]> * 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 <[email protected]> Co-authored-by: Santanu Roy <[email protected]> * refactor(declarations): move declaration uniqueness check to model file (#794) * refactor(declarations): Move unique name check to model file. Signed-off-by: Ertugrul Karademir <[email protected]> * refactor(test): move validation checks for duplicate class to model file Signed-off-by: Ertugrul Karademir <[email protected]> * test: empty commit to trigger test Signed-off-by: Ertugrul Karademir <[email protected]> --------- Signed-off-by: Ertugrul Karademir <[email protected]> * perf(core): don't use arrays to check uniqueness (#802) refactor: don't use arrays to check uniqueness Signed-off-by: Ertugrul Karademir <[email protected]> * 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 <[email protected]> * refactor: also refactor unique property name check Signed-off-by: Ertugrul Karademir <[email protected]> * refactor: remove array for decorator uniqueness check Signed-off-by: Ertugrul Karademir <[email protected]> * refactor: remove uniqueness check from scalar declarations as well Signed-off-by: Ertugrul Karademir <[email protected]> --------- Signed-off-by: Ertugrul Karademir <[email protected]> --------- Signed-off-by: Matt Roberts <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Stefan Blaginov <[email protected]> Signed-off-by: Ertugrul Karademir <[email protected]> Signed-off-by: Santanu Roy <[email protected]> Co-authored-by: Stefan Blaginov <[email protected]> Co-authored-by: Stefan Blaginov <[email protected]> Co-authored-by: Ertugrul Karademir <[email protected]> Co-authored-by: Santanu Roy <[email protected]> Co-authored-by: Santanu Roy <[email protected]>
Closes #772
Changes
Adds type to validator error in introspect validator.
BaseException
which can takeerrorType
as well aserrorMessage
which helps to determine what kind of error is getting thrown from the validator itself. and using that to throw error invalidator
.NotFoundException
for better complienceAuthor Checklist
--signoff
option of git commit.main
fromfork:branchname