Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Unifying errors in parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
moozzyk authored and moozzyk committed Mar 20, 2017
1 parent 33c94c1 commit 913354f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand Down
18 changes: 9 additions & 9 deletions client-ts/Microsoft.AspNetCore.SignalR.Client.TS/Formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,15 +36,15 @@ 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) {
// We need to decode and put in an ArrayBuffer. Throw for now
// 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;

Expand All @@ -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)];
Expand All @@ -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 = [];
Expand Down

0 comments on commit 913354f

Please sign in to comment.