Merge pull request #21 from mzgoddard/export-assert-error

feat: export AssertionError and ValidationError
This commit is contained in:
Karishma Chadha 2018-12-20 09:50:27 -05:00 committed by GitHub
commit 6a8cac3829
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 5 deletions

View file

@ -1 +1,2 @@
export {SB1File} from './sb1-file'; export {SB1File} from './sb1-file';
export {AssertionError, ValidationError} from './util/assert';

View file

@ -27,7 +27,7 @@ class SB1Signature extends Packet.extend({
* @throws {AssertionError} Throws if it is not valid. * @throws {AssertionError} Throws if it is not valid.
*/ */
validate () { validate () {
assert( assert.validate(
this.equals({version: 'ScratchV01'}) || this.equals({version: 'ScratchV01'}) ||
this.equals({version: 'ScratchV02'}), this.equals({version: 'ScratchV02'}),
'Invalid Scratch file signature.' 'Invalid Scratch file signature.'
@ -45,7 +45,7 @@ class SB1Header extends Packet.extend({
numObjects: Uint32BE numObjects: Uint32BE
}) { }) {
validate () { validate () {
assert( assert.validate(
this.equals({ this.equals({
ObjS: 'ObjS', ObjS: 'ObjS',
ObjSValue: 1, ObjSValue: 1,

View file

@ -3,11 +3,21 @@
*/ */
class AssertionError extends Error {} class AssertionError extends Error {}
/**
* A `scratch-sb1-converter` validation error.
*/
class ValidationError extends AssertionError {}
const assert = function (test, message) { const assert = function (test, message) {
if (!test) throw new AssertionError(message); if (!test) throw new AssertionError(message);
}; };
export { assert.validate = function (test, message) {
AssertionError, if (!test) throw new ValidationError(message);
assert };
export {
assert,
AssertionError,
ValidationError
}; };