Unifying errors in parsers

This commit is contained in:
Pawel Kadluczka 2017-03-17 17:13:32 -07:00 committed by moozzyk
parent 33c94c1a47
commit 913354f688
2 changed files with 25 additions and 25 deletions

View File

@ -20,18 +20,18 @@ describe("Text Message Formatter", () => {
});
([
["TABC", "Invalid length: 'ABC'"],
["X1:T:A", "Unsupported message format: 'X'"],
["T1:T:A;12ab34:", "Invalid length: '12ab34'"],
["T1:T:A;1:asdf:", "Unknown type value: 'asdf'"],
["T1:T:A;1::", "Message is incomplete"],
["T1:T:A;1:AB:", "Message is incomplete"],
["T1:T:A;5:T:A", "Message is incomplete"],
["T1:T:A;5:T:AB", "Message is incomplete"],
["T1:T:A;5:T:ABCDE", "Message is incomplete"],
["T1:T:A;5:X:ABCDE", "Message is incomplete"],
["T1:T:A;5:T:ABCDEF", "Message missing trailer character"],
] as [[string, string]]).forEach(([payload, expected_error]) => {
["TABC", new Error("Invalid length: 'ABC'")],
["X1:T:A", new Error("Unsupported message format: 'X'")],
["T1:T:A;12ab34:", new Error("Invalid length: '12ab34'")],
["T1:T:A;1:asdf:", new Error("Unknown type value: 'asdf'")],
["T1:T:A;1::", new Error("Message is incomplete")],
["T1:T:A;1:AB:", new Error("Message is incomplete")],
["T1:T:A;5:T:A", new Error("Message is incomplete")],
["T1:T:A;5:T:AB", new Error("Message is incomplete")],
["T1:T:A;5:T:ABCDE", new Error("Message is incomplete")],
["T1:T:A;5:X:ABCDE", new Error("Message is incomplete")],
["T1:T:A;5:T:ABCDEF", new Error("Message missing trailer character")],
] as [[string, Error]]).forEach(([payload, expected_error]) => {
it(`should fail to parse '${payload}'`, () => {
expect(() => TextMessageFormat.parse(payload)).toThrow(expected_error);
});
@ -40,10 +40,10 @@ describe("Text Message Formatter", () => {
describe("Server-Sent Events Formatter", () => {
([
["", "Message is missing header"],
["A", "Unknown type value: 'A'"],
["BOO\r\nBlarg", "Unknown type value: 'BOO'"]
] as [string, string][]).forEach(([payload, expected_error]) => {
["", new Error("Message is missing header")],
["A", new Error("Unknown type value: 'A'")],
["BOO\r\nBlarg", new Error("Unknown type value: 'BOO'")]
] as [string, Error][]).forEach(([payload, expected_error]) => {
it(`should fail to parse '${payload}`, () => {
expect(() => ServerSentEventsFormat.parse(payload)).toThrow(expected_error);
});

View File

@ -22,7 +22,7 @@ export namespace ServerSentEventsFormat {
// Binary messages require Base64-decoding and ArrayBuffer support, just like in the other formats below
if (input.length == 0) {
throw "Message is missing header";
throw new Error("Message is missing header");
}
let [header, offset] = splitAt(input, "\n", 0);
@ -36,7 +36,7 @@ export namespace ServerSentEventsFormat {
// Parse the header
var messageType = knownTypes[header];
if (messageType === undefined) {
throw "Unknown type value: '" + header + "'";
throw new Error(`Unknown type value: '${header}'`);
}
if (messageType == MessageType.Binary) {
@ -44,7 +44,7 @@ export namespace ServerSentEventsFormat {
// This will require our own Base64-decoder because the browser
// built-in one only decodes to strings and throws if invalid UTF-8
// characters are found.
throw "TODO: Support for binary messages";
throw new Error("TODO: Support for binary messages");
}
// Create the message
@ -70,13 +70,13 @@ export namespace TextMessageFormat {
// parseInt is too leniant, we need a strict check to see if the string is an int
if (!LengthRegex.test(lenStr)) {
throw `Invalid length: '${lenStr}'`;
throw new Error(`Invalid length: '${lenStr}'`);
}
let length = Number.parseInt(lenStr);
// Required space is: 3 (type flag, ":", ";") + length (payload len)
if (!hasSpace(input, offset, 3 + length)) {
throw "Message is incomplete";
throw new Error("Message is incomplete");
}
// Read the type
@ -85,7 +85,7 @@ export namespace TextMessageFormat {
// Parse the type
var messageType = knownTypes[typeStr];
if (messageType === undefined) {
throw "Unknown type value: '" + typeStr + "'";
throw new Error(`Unknown type value: '${typeStr}'`);
}
// Read the payload
@ -94,7 +94,7 @@ export namespace TextMessageFormat {
// Verify the final trailing character
if (input[offset] != ';') {
throw "Message missing trailer character";
throw new Error("Message missing trailer character");
}
offset += 1;
@ -103,7 +103,7 @@ export namespace TextMessageFormat {
// This will require our own Base64-decoder because the browser
// built-in one only decodes to strings and throws if invalid UTF-8
// characters are found.
throw "TODO: Support for binary messages";
throw new Error("TODO: Support for binary messages");
}
return [offset, new Message(messageType, payload)];
@ -115,7 +115,7 @@ export namespace TextMessageFormat {
}
if (input[0] != 'T') {
throw `Unsupported message format: '${input[0]}'`;
throw new Error(`Unsupported message format: '${input[0]}'`);
}
let messages = [];