Skip to content

Instantly share code, notes, and snippets.

@swashcap
Created April 25, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swashcap/1dafa2df60d6446e7dd6d66e32fa605e to your computer and use it in GitHub Desktop.
Save swashcap/1dafa2df60d6446e7dd6d66e32fa605e to your computer and use it in GitHub Desktop.
Testing error sanitization
'use strict';
const assert = require('assert');
class ModelsError extends Error {
constructor(message, file, fileType) {
super(message);
this.name = 'ModelsError';
this.file = file;
this.fileType = fileType;
}
sanitize() {
return Object.getOwnPropertyNames(this).reduce((sanitized, prop) => {
sanitized[prop] = prop === 'file' || prop === 'stack' ?
'sanitized' :
this[prop];
return sanitized;
}, {});
}
}
const e = new ModelsError(
'To error is to COINS',
'/path/to/bad.nifti',
'NIFTI'
);
try {
assert.deepEqual(
e.sanitize(),
{
file: 'sanitized',
fileType: 'NIFTI',
message: 'To error is to COINS',
name: 'ModelsError',
stack: 'sanitized',
}
);
console.log('👍 Sanitized');
} catch (error) {
console.error('😮 Unsanitized', error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment