Skip to content

Commit

Permalink
Update error handling.
Browse files Browse the repository at this point in the history
- Use `serialize-error` and allow list of error keys when create error.
- More alignment with bedrock-vc-delivery error code.
  • Loading branch information
davidlehn committed Aug 27, 2024
1 parent a667db8 commit 356d5fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import {getDocumentStore} from './helpers.js';
import {issue} from './issuer.js';
import {issueCredentialBody} from '../schemas/bedrock-vc-issuer.js';
import {logger} from './logger.js';
import {serializeError} from 'serialize-error';
import {createValidateMiddleware as validate} from '@bedrock/validation';

const {util: {BedrockError}} = bedrock;

const ALLOWED_ERROR_KEYS = [
'message', 'name', 'type', 'data', 'errors', 'error', 'details', 'cause',
'status'
];

// FIXME: remove and apply at top-level application
bedrock.events.on('bedrock-express.configure.bodyParser', app => {
app.use(bodyParser.json({
Expand Down Expand Up @@ -147,25 +153,26 @@ function _throwWrappedError({cause}) {
}

function _stripStackTrace(error) {
// copy error data
const stripped = {...error};
if(error.name) {
stripped.name = error.name;
}
if(error.message) {
stripped.message = error.message;
// serialize error and allow-list specific properties
const serialized = serializeError(error);
const _error = {};
for(const key of ALLOWED_ERROR_KEYS) {
if(serialized[key] !== undefined) {
_error[key] = serialized[key];
}
}
// remove stack
delete stripped.stack;
// strip other potential stack data
if(stripped.errors) {
stripped.errors = stripped.errors.map(_stripStackTrace);
if(_error.errors) {
_error.errors = _error.errors.map(_stripStackTrace);
}
if(Array.isArray(_error.details?.errors)) {
_error.details.errors = _error.details.errors.map(_stripStackTrace);
}
if(stripped.cause) {
stripped.cause = _stripStackTrace(stripped.cause);
if(_error.cause) {
_error.cause = _stripStackTrace(_error.cause);
}
if(stripped.details?.cause) {
stripped.details.cause = _stripStackTrace(stripped.details.cause);
if(_error.details?.cause) {
_error.details.cause = _stripStackTrace(_error.details.cause);
}
return stripped;
return _error;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"cors": "^2.8.5",
"klona": "^2.0.6",
"lru-cache": "^6.0.0",
"serialize-error": "^11.0.3",
"uuid": "^10.0.0"
},
"peerDependencies": {
Expand Down

0 comments on commit 356d5fb

Please sign in to comment.