Unifying errors in parsers
This commit is contained in:
parent
33c94c1a47
commit
913354f688
|
|
@ -20,18 +20,18 @@ describe("Text Message Formatter", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
([
|
([
|
||||||
["TABC", "Invalid length: 'ABC'"],
|
["TABC", new Error("Invalid length: 'ABC'")],
|
||||||
["X1:T:A", "Unsupported message format: 'X'"],
|
["X1:T:A", new Error("Unsupported message format: 'X'")],
|
||||||
["T1:T:A;12ab34:", "Invalid length: '12ab34'"],
|
["T1:T:A;12ab34:", new Error("Invalid length: '12ab34'")],
|
||||||
["T1:T:A;1:asdf:", "Unknown type value: 'asdf'"],
|
["T1:T:A;1:asdf:", new Error("Unknown type value: 'asdf'")],
|
||||||
["T1:T:A;1::", "Message is incomplete"],
|
["T1:T:A;1::", new Error("Message is incomplete")],
|
||||||
["T1:T:A;1:AB:", "Message is incomplete"],
|
["T1:T:A;1:AB:", new Error("Message is incomplete")],
|
||||||
["T1:T:A;5:T:A", "Message is incomplete"],
|
["T1:T:A;5:T:A", new Error("Message is incomplete")],
|
||||||
["T1:T:A;5:T:AB", "Message is incomplete"],
|
["T1:T:A;5:T:AB", new Error("Message is incomplete")],
|
||||||
["T1:T:A;5:T:ABCDE", "Message is incomplete"],
|
["T1:T:A;5:T:ABCDE", new Error("Message is incomplete")],
|
||||||
["T1:T:A;5:X:ABCDE", "Message is incomplete"],
|
["T1:T:A;5:X:ABCDE", new Error("Message is incomplete")],
|
||||||
["T1:T:A;5:T:ABCDEF", "Message missing trailer character"],
|
["T1:T:A;5:T:ABCDEF", new Error("Message missing trailer character")],
|
||||||
] as [[string, string]]).forEach(([payload, expected_error]) => {
|
] as [[string, Error]]).forEach(([payload, expected_error]) => {
|
||||||
it(`should fail to parse '${payload}'`, () => {
|
it(`should fail to parse '${payload}'`, () => {
|
||||||
expect(() => TextMessageFormat.parse(payload)).toThrow(expected_error);
|
expect(() => TextMessageFormat.parse(payload)).toThrow(expected_error);
|
||||||
});
|
});
|
||||||
|
|
@ -40,10 +40,10 @@ describe("Text Message Formatter", () => {
|
||||||
|
|
||||||
describe("Server-Sent Events Formatter", () => {
|
describe("Server-Sent Events Formatter", () => {
|
||||||
([
|
([
|
||||||
["", "Message is missing header"],
|
["", new Error("Message is missing header")],
|
||||||
["A", "Unknown type value: 'A'"],
|
["A", new Error("Unknown type value: 'A'")],
|
||||||
["BOO\r\nBlarg", "Unknown type value: 'BOO'"]
|
["BOO\r\nBlarg", new Error("Unknown type value: 'BOO'")]
|
||||||
] as [string, string][]).forEach(([payload, expected_error]) => {
|
] as [string, Error][]).forEach(([payload, expected_error]) => {
|
||||||
it(`should fail to parse '${payload}`, () => {
|
it(`should fail to parse '${payload}`, () => {
|
||||||
expect(() => ServerSentEventsFormat.parse(payload)).toThrow(expected_error);
|
expect(() => ServerSentEventsFormat.parse(payload)).toThrow(expected_error);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export namespace ServerSentEventsFormat {
|
||||||
// Binary messages require Base64-decoding and ArrayBuffer support, just like in the other formats below
|
// Binary messages require Base64-decoding and ArrayBuffer support, just like in the other formats below
|
||||||
|
|
||||||
if (input.length == 0) {
|
if (input.length == 0) {
|
||||||
throw "Message is missing header";
|
throw new Error("Message is missing header");
|
||||||
}
|
}
|
||||||
|
|
||||||
let [header, offset] = splitAt(input, "\n", 0);
|
let [header, offset] = splitAt(input, "\n", 0);
|
||||||
|
|
@ -36,7 +36,7 @@ export namespace ServerSentEventsFormat {
|
||||||
// Parse the header
|
// Parse the header
|
||||||
var messageType = knownTypes[header];
|
var messageType = knownTypes[header];
|
||||||
if (messageType === undefined) {
|
if (messageType === undefined) {
|
||||||
throw "Unknown type value: '" + header + "'";
|
throw new Error(`Unknown type value: '${header}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messageType == MessageType.Binary) {
|
if (messageType == MessageType.Binary) {
|
||||||
|
|
@ -44,7 +44,7 @@ export namespace ServerSentEventsFormat {
|
||||||
// This will require our own Base64-decoder because the browser
|
// This will require our own Base64-decoder because the browser
|
||||||
// built-in one only decodes to strings and throws if invalid UTF-8
|
// built-in one only decodes to strings and throws if invalid UTF-8
|
||||||
// characters are found.
|
// characters are found.
|
||||||
throw "TODO: Support for binary messages";
|
throw new Error("TODO: Support for binary messages");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the message
|
// 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
|
// parseInt is too leniant, we need a strict check to see if the string is an int
|
||||||
|
|
||||||
if (!LengthRegex.test(lenStr)) {
|
if (!LengthRegex.test(lenStr)) {
|
||||||
throw `Invalid length: '${lenStr}'`;
|
throw new Error(`Invalid length: '${lenStr}'`);
|
||||||
}
|
}
|
||||||
let length = Number.parseInt(lenStr);
|
let length = Number.parseInt(lenStr);
|
||||||
|
|
||||||
// Required space is: 3 (type flag, ":", ";") + length (payload len)
|
// Required space is: 3 (type flag, ":", ";") + length (payload len)
|
||||||
if (!hasSpace(input, offset, 3 + length)) {
|
if (!hasSpace(input, offset, 3 + length)) {
|
||||||
throw "Message is incomplete";
|
throw new Error("Message is incomplete");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the type
|
// Read the type
|
||||||
|
|
@ -85,7 +85,7 @@ export namespace TextMessageFormat {
|
||||||
// Parse the type
|
// Parse the type
|
||||||
var messageType = knownTypes[typeStr];
|
var messageType = knownTypes[typeStr];
|
||||||
if (messageType === undefined) {
|
if (messageType === undefined) {
|
||||||
throw "Unknown type value: '" + typeStr + "'";
|
throw new Error(`Unknown type value: '${typeStr}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the payload
|
// Read the payload
|
||||||
|
|
@ -94,7 +94,7 @@ export namespace TextMessageFormat {
|
||||||
|
|
||||||
// Verify the final trailing character
|
// Verify the final trailing character
|
||||||
if (input[offset] != ';') {
|
if (input[offset] != ';') {
|
||||||
throw "Message missing trailer character";
|
throw new Error("Message missing trailer character");
|
||||||
}
|
}
|
||||||
offset += 1;
|
offset += 1;
|
||||||
|
|
||||||
|
|
@ -103,7 +103,7 @@ export namespace TextMessageFormat {
|
||||||
// This will require our own Base64-decoder because the browser
|
// This will require our own Base64-decoder because the browser
|
||||||
// built-in one only decodes to strings and throws if invalid UTF-8
|
// built-in one only decodes to strings and throws if invalid UTF-8
|
||||||
// characters are found.
|
// characters are found.
|
||||||
throw "TODO: Support for binary messages";
|
throw new Error("TODO: Support for binary messages");
|
||||||
}
|
}
|
||||||
|
|
||||||
return [offset, new Message(messageType, payload)];
|
return [offset, new Message(messageType, payload)];
|
||||||
|
|
@ -115,7 +115,7 @@ export namespace TextMessageFormat {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (input[0] != 'T') {
|
if (input[0] != 'T') {
|
||||||
throw `Unsupported message format: '${input[0]}'`;
|
throw new Error(`Unsupported message format: '${input[0]}'`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let messages = [];
|
let messages = [];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue