Skip to content

Commit

Permalink
Improve cause tracking and fix path adjustment code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Oct 22, 2023
1 parent 0e53990 commit bbdc42e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
15 changes: 5 additions & 10 deletions lib/OID4Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ export class OID4Client {
// if `didProofSigner` is not provided, throw error
if(!(did && didProofSigner)) {
const {data: details} = cause;
const error = new Error('DID authentication is required.');
const error = new Error('DID authentication is required.', {cause});
error.name = 'NotAllowedError';
error.cause = cause;
error.details = details;
throw error;
}
Expand Down Expand Up @@ -188,9 +187,8 @@ export class OID4Client {
*/
return result;
} catch(cause) {
const error = new Error('Could not receive credentials.');
const error = new Error('Could not receive credentials.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand All @@ -205,10 +203,8 @@ export class OID4Client {
if(parsedIssuer.protocol !== 'https:') {
throw new Error('Only "https" credential issuer URLs are supported.');
}
} catch(e) {
const err = new Error('"offer.credential_issuer" is not valid.');
err.cause = e;
throw err;
} catch(cause) {
throw new Error('"offer.credential_issuer" is not valid.', {cause});
}
if(!(Array.isArray(credentials) && credentials.length > 0 &&
credentials.every(c => c && typeof c === 'object'))) {
Expand Down Expand Up @@ -303,9 +299,8 @@ export class OID4Client {
return new OID4Client(
{accessToken, agent, issuerConfig, metadata, offer});
} catch(cause) {
const error = new Error('Could not create OID4 client.');
const error = new Error('Could not create OID4 client.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand Down
25 changes: 8 additions & 17 deletions lib/oid4vp.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ export async function getAuthorizationRequest({
// return merged authorization request and original response
return {authorizationRequest, fetched: true, requestUrl, response, jwt};
} catch(cause) {
const error = new Error('Could not get authorization request.');
const error = new Error('Could not get authorization request.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand Down Expand Up @@ -192,9 +191,8 @@ export async function sendAuthorizationResponse({
return {result};
} catch(cause) {
const error = new Error(
'Could not send OID4VP authorization response.');
'Could not send OID4VP authorization response.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand Down Expand Up @@ -291,9 +289,8 @@ export async function toVpr({
} catch(cause) {
const error = new Error(
'Could not convert OID4VP authorization request to ' +
'verifiable presentation request.');
'verifiable presentation request.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand Down Expand Up @@ -371,9 +368,8 @@ export function fromVpr({verifiablePresentationRequest, strict = false} = {}) {
} catch(cause) {
const error = new Error(
'Could not convert verifiable presentation request to ' +
'an OID4VP authorization request.');
'an OID4VP authorization request.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}
}
Expand Down Expand Up @@ -425,9 +421,8 @@ export function createPresentationSubmission({
}
} catch(cause) {
const error = new Error(
'Could not create presentation submission.');
'Could not create presentation submission.', {cause});
error.name = 'OperationError';
error.cause = cause;
throw error;
}

Expand Down Expand Up @@ -553,9 +548,8 @@ function _matchesInputDescriptor({
} catch(cause) {
const id = field.id || (JSON.stringify(field).slice(0, 50) + ' ...');
const error = new Error(
`Could not process input descriptor field: "${id}".`);
`Could not process input descriptor field: "${id}".`, {cause});
error.field = field;
error.cause = cause;
throw error;
}
}
Expand Down Expand Up @@ -721,9 +715,8 @@ function _toQueryByExampleQuery({inputDescriptor, strict = false}) {
} catch(cause) {
const id = field.id || (JSON.stringify(field).slice(0, 50) + ' ...');
const error = new Error(
`Could not process input descriptor field: "${id}".`);
`Could not process input descriptor field: "${id}".`, {cause});
error.field = field;
error.cause = cause;
throw error;
}
}
Expand All @@ -737,9 +730,7 @@ function _adjustErroneousPaths(paths) {
// JWT-secured VC, such that only actual VC paths remain
const removed = paths.filter(p => !_isPresentationSubmissionPath(p));
return removed.map(p => {
if(_isJWTPath(p)) {
return '$' + p.slice('$.vc'.length);
}
return !_isJWTPath(p) ? p : '$' + p.slice('$.vc'.length);
});
}

Expand Down

0 comments on commit bbdc42e

Please sign in to comment.