Created
April 25, 2017 21:10
-
-
Save swashcap/1dafa2df60d6446e7dd6d66e32fa605e to your computer and use it in GitHub Desktop.
Testing error sanitization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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