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.
This commit is contained in:
Matthew Raymer
2025-05-28 09:00:46 +00:00
parent 7166dadbc0
commit 137fce3e30

View File

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