diff --git a/src/index.js b/src/index.js index 8d3c008..43f935f 100644 --- a/src/index.js +++ b/src/index.js @@ -1 +1,2 @@ export {SB1File} from './sb1-file'; +export {AssertionError, ValidationError} from './util/assert'; diff --git a/src/sb1-file-packets.js b/src/sb1-file-packets.js index 4de1649..07b5efe 100644 --- a/src/sb1-file-packets.js +++ b/src/sb1-file-packets.js @@ -27,7 +27,7 @@ class SB1Signature extends Packet.extend({ * @throws {AssertionError} Throws if it is not valid. */ validate () { - assert( + assert.validate( this.equals({version: 'ScratchV01'}) || this.equals({version: 'ScratchV02'}), 'Invalid Scratch file signature.' @@ -45,7 +45,7 @@ class SB1Header extends Packet.extend({ numObjects: Uint32BE }) { validate () { - assert( + assert.validate( this.equals({ ObjS: 'ObjS', ObjSValue: 1, diff --git a/src/util/assert.js b/src/util/assert.js index cf0ec11..5704da2 100644 --- a/src/util/assert.js +++ b/src/util/assert.js @@ -3,11 +3,21 @@ */ class AssertionError extends Error {} +/** + * A `scratch-sb1-converter` validation error. + */ +class ValidationError extends AssertionError {} + const assert = function (test, message) { if (!test) throw new AssertionError(message); }; -export { - AssertionError, - assert +assert.validate = function (test, message) { + if (!test) throw new ValidationError(message); +}; + +export { + assert, + AssertionError, + ValidationError };