Skip to content

Conversation

javereec
Copy link
Contributor

@javereec javereec commented Feb 9, 2024

Just an idea I had to improve how errors can be extended with codes allowing for better handling by clients.

I think it also fixes some other issues in the specs

@vijayshiyani
Copy link
Contributor

Good idea, I changed how error messages structure.
TO DO:

  • Fix error messages
  • Fix tests

@linasi
Copy link
Collaborator

linasi commented Feb 13, 2024

Looks good. I would suggest to keep it a bit more simple.

  • extra_info related functionality should be dropped
  • remove getResponse method, error message is available via error.message that comes form the base class.
  • remove equals method

I am not so sure about having code and errorType attributes together. They seem to overlap a little. Are there any rules on how they are defined? Wouldn't just having one be enough? Also, shouldn't code be a unique value?

@leikind
Copy link

leikind commented Jun 26, 2025

@CodiumAI-Agent /improve

@leikind
Copy link

leikind commented Jun 27, 2025

PR Code Suggestions ✨

Latest suggestions up to 56462c7

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix method name typo

The method call validateHahserConfig contains a typo and should be
validateHasherConfig to match the corrected method name.

src/issuer.ts [19-24]

 constructor(signer: SignerConfig, hasher: HasherConfig) {
   this.validateSignerConfig(signer);
-  this.validateHahserConfig(hasher);
+  this.validateHasherConfig(hasher);
   this.signer = signer;
   this.hasher = hasher;
 }
Suggestion importance[1-10]: 8

__

Why: This suggestion correctly identifies a critical typo in the method call validateHahserConfig that would cause a runtime error since the method doesn't exist. This is a high-impact bug fix that prevents the code from running.

Medium
Fix unreliable object comparison method

Using JSON.stringify for object comparison is unreliable due to property ordering
differences. This can cause false negatives when comparing objects with identical
content but different key orders.

src/errors.ts [43-48]

 equals(exception: SDJWTVCError) {
   return (
     this.getErrorType() === exception.getErrorType() &&
-    JSON.stringify(this.getExtraInfo()) === JSON.stringify(exception.getExtraInfo())
+    this.deepEqual(this.getExtraInfo(), exception.getExtraInfo())
   );
 }
 
+private deepEqual(obj1: any, obj2: any): boolean {
+  if (obj1 === obj2) return true;
+  if (obj1 == null || obj2 == null) return false;
+  if (typeof obj1 !== typeof obj2) return false;
+  
+  if (typeof obj1 === 'object') {
+    const keys1 = Object.keys(obj1);
+    const keys2 = Object.keys(obj2);
+    if (keys1.length !== keys2.length) return false;
+    return keys1.every(key => this.deepEqual(obj1[key], obj2[key]));
+  }
+  
+  return false;
+}
+
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies that using JSON.stringify for object comparison is unreliable due to property ordering. However, the improved code introduces a complex deepEqual method when simpler alternatives like isDeepStrictEqual from Node.js util module (already imported in the test file) could be used.

Medium
General
Fix typo and consolidate validation logic

The method name validateHahserConfig contains a typo and should be
validateHasherConfig. Additionally, both methods have identical logic and could be
consolidated into a single reusable method.

src/issuer.ts [25-41]

-private validateSignerConfig(config: SignerConfig | HasherConfig) {
+private validateSignerConfig(config: SignerConfig) {
+  this.validateConfig(config, 'signer');
+}
+
+private validateHasherConfig(config: HasherConfig) {
+  this.validateConfig(config, 'hasher');
+}
+
+private validateConfig(config: SignerConfig | HasherConfig, type: 'signer' | 'hasher') {
   if (!config.callback || typeof config.callback !== 'function') {
-    throw new SDJWTVCError('signer_callback_function_is_required');
+    throw new SDJWTVCError(`${type}_callback_function_is_required`);
   }
   if (!config.alg || typeof config.alg !== 'string') {
-    throw new SDJWTVCError('signer_algorithm_is_required');
+    throw new SDJWTVCError(`${type}_algorithm_is_required`);
   }
 }
 
-private validateHahserConfig(config: SignerConfig | HasherConfig) {
-  if (!config.callback || typeof config.callback !== 'function') {
-    throw new SDJWTVCError('hasher_callback_function_is_required');
-  }
-  if (!config.alg || typeof config.alg !== 'string') {
-    throw new SDJWTVCError('hasher_algorithm_is_required');
-  }
-}
-
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies the typo in validateHahserConfig and proposes consolidating duplicate validation logic. While the consolidation improves maintainability, the impact is moderate since the methods are private and the typo doesn't affect functionality.

Low

Previous suggestions

Suggestions up to commit 56462c7
CategorySuggestion                                                                                                                                    Impact
Possible issue
Correct hasher config method typo

There’s a typo in the method name validateHahserConfig and its invocation, causing
potential runtime failures. Rename both occurrences to validateHasherConfig so the
method is correctly referenced and invoked.

src/issuer.ts [21-34]

-this.validateHahserConfig(hasher);
-private validateHahserConfig(config: SignerConfig | HasherConfig) {
+this.validateHasherConfig(hasher);
+private validateHasherConfig(config: SignerConfig | HasherConfig) {
Suggestion importance[1-10]: 8

__

Why: Fixing the typo in validateHahserConfig is crucial to avoid runtime errors when initializing the issuer and ensures the hasher config validation is actually invoked.

Medium
General
Remove test-utils import

The production Holder class should not depend on test utilities. Remove the import
of hasherCallbackFn from ./test-utils/helpers.js to eliminate unintended test-only
dependencies.

src/holder.ts [3]

-import { hasherCallbackFn } from './test-utils/helpers.js';
 
+
Suggestion importance[1-10]: 5

__

Why: Dropping hasherCallbackFn from production code prevents unintended test-only dependencies, improving module isolation, though its impact is minor.

Low
Strengthen equals comparison

The equals method lacks an explicit return type and uses JSON.stringify for object
comparison, which can fail on key order differences. Add a : boolean return
annotation and replace the string comparison with a deep comparison using key-by-key
checks.

src/errors.ts [43-47]

-equals(exception: SDJWTVCError) {
-  return (
-    this.getErrorType() === exception.getErrorType() &&
-    JSON.stringify(this.getExtraInfo()) === JSON.stringify(exception.getExtraInfo())
-  );
+equals(exception: SDJWTVCError): boolean {
+  const a = this.getExtraInfo();
+  const b = exception.getExtraInfo();
+  const sameKeys = Object.keys(a).length === Object.keys(b).length &&
+                   Object.keys(a).every(key => a[key] === b[key]);
+  return this.getErrorType() === exception.getErrorType() && sameKeys;
 }
Suggestion importance[1-10]: 4

__

Why: Adding a boolean return type and replacing JSON.stringify with a deep key-by-key check improves correctness, but this method is rarely critical and the change is mainly stylistic.

Low

@Meeco Meeco deleted a comment from CodiumAI-Agent Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants