mirror of
https://github.com/scratchfoundation/scratch-sb1-converter.git
synced 2024-11-24 00:28:08 -05:00
feat: export AssertionError and ValidationError
Add ValidationError to help distinguish between unrecoverable errors while decoding an `.sb` file and encoding to Scratch 2 data. Export AssertionError and ValidationError so users of the converter can know when an error is from validating the Scratch 1 file (as in it likely is not an Scratch 1 file) or from making sure the Scratch 1 file data matches expected ranges (like not making memory buffers that are too big).
This commit is contained in:
parent
22989c084f
commit
9231957791
3 changed files with 16 additions and 5 deletions
|
@ -1 +1,2 @@
|
||||||
export {SB1File} from './sb1-file';
|
export {SB1File} from './sb1-file';
|
||||||
|
export {AssertionError, ValidationError} from './util/assert';
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue