Browse Source

fix(endorserServer): improve type safety and error handling

- Update claimSummary to handle both GenericVerifiableCredential and GenericCredWrapper types
- Fix logger error handling in register function to properly stringify response data
- Add type narrowing with 'claim' in claim check for safer type handling
- Improve error message formatting for registration errors

This change improves type safety by properly handling different claim types
and ensures consistent error logging. The registration error handling now
properly stringifies response data for better debugging.
pull/137/head
Matthew Raymer 2 weeks ago
parent
commit
137fce3e30
  1. 17
      src/libs/endorserServer.ts

17
src/libs/endorserServer.ts

@ -1106,21 +1106,20 @@ export const capitalizeAndInsertSpacesBeforeCaps = (text: string) => {
similar code is also contained in endorser-mobile
**/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const claimSummary = (
claim: GenericCredWrapper<GenericVerifiableCredential>,
claim: GenericVerifiableCredential | GenericCredWrapper<GenericVerifiableCredential>,
) => {
if (!claim) {
// to differentiate from "something" above
return "something";
}
let specificClaim:
| GenericVerifiableCredential
| GenericCredWrapper<GenericVerifiableCredential> = claim;
if (claim.claim) {
// probably a Verified Credential
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let specificClaim: GenericVerifiableCredential;
if ('claim' in claim) {
// It's a GenericCredWrapper
specificClaim = claim.claim;
} else {
// It's already a GenericVerifiableCredential
specificClaim = claim;
}
if (Array.isArray(specificClaim)) {
if (specificClaim.length === 1) {
@ -1339,7 +1338,7 @@ export async function register(
}
return { error: message };
} else {
logger.error(resp);
logger.error("Registration error:", JSON.stringify(resp.data));
return { error: "Got a server error when registering." };
}
}

Loading…
Cancel
Save