Adjust how error message is caught and displayed

This commit is contained in:
NI
2019-09-14 21:30:49 +08:00
parent 6b5c444f45
commit d9d0170d1a
8 changed files with 94 additions and 60 deletions

View File

@@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export default class Exception { export default class Exception extends Error {
/** /**
* constructor * constructor
* *
@@ -23,16 +23,6 @@ export default class Exception {
* *
*/ */
constructor(message) { constructor(message) {
this.message = message; super(message);
}
/**
* Return the error string
*
* @returns {string} Error message
*
*/
toString() {
return this.message;
} }
} }

View File

@@ -33,7 +33,7 @@ export async function hmac512(secret, data) {
["sign", "verify"] ["sign", "verify"]
); );
return crypto.subtle.sign("HMAC", key, data); return crypto.subtle.sign(key.algorithm, key, data);
} }
export const GCMNonceSize = 12; export const GCMNonceSize = 12;

View File

@@ -167,7 +167,7 @@ export function build(ctx) {
this.message = "ERR"; this.message = "ERR";
this.classStyle = "red flash"; this.classStyle = "red flash";
this.windowClass = "red"; this.windowClass = "red";
this.status.description = connectionStatusDisconnected + ". Error: " + e; this.status.description = connectionStatusDisconnected + ": " + e;
}, },
failed(e) { failed(e) {
ctx.connector.inputting = false; ctx.connector.inputting = false;

View File

@@ -74,7 +74,7 @@ class Dial {
return reject(e); return reject(e);
}; };
ws.addEventListener("open", event => { ws.addEventListener("open", _event => {
myRes(ws); myRes(ws);
}); });
@@ -86,7 +86,7 @@ class Dial {
myRej(event); myRej(event);
}); });
ws.addEventListener("error", event => { ws.addEventListener("error", _event => {
ws.close(); ws.close();
}); });
}); });
@@ -143,20 +143,7 @@ class Dial {
bufferReader.readAsArrayBuffer(data); bufferReader.readAsArrayBuffer(data);
}); });
}), });
sdDataConvert = rawData => {
return rawData;
},
sd = new sender.Sender(
async rawData => {
let data = await sdDataConvert(rawData);
ws.send(data.buffer);
callbacks.outbound(data);
},
15,
4096 - 64 // Server has a 4096 bytes receive buffer, can be no greater
);
ws.addEventListener("message", event => { ws.addEventListener("message", event => {
callbacks.inbound(event.data); callbacks.inbound(event.data);
@@ -165,13 +152,45 @@ class Dial {
}); });
ws.addEventListener("error", event => { ws.addEventListener("error", event => {
rd.close(); event.toString = () => {
return "WebSocket Error (" + event.code + ")";
};
rd.closeWithReason(event);
}); });
ws.addEventListener("close", event => { ws.addEventListener("close", _event => {
rd.close(); rd.closeWithReason("Connection is closed");
}); });
let sdDataConvert = rawData => {
return rawData;
},
getSdDataConvert = () => {
return sdDataConvert;
},
sd = new sender.Sender(
async rawData => {
try {
let data = await getSdDataConvert()(rawData);
ws.send(data.buffer);
callbacks.outbound(data);
} catch (e) {
ws.close();
rd.closeWithReason(e);
if (process.env.NODE_ENV === "development") {
console.error(e);
}
throw e;
}
},
15,
4096 - 64 // Server has a 4096 bytes receive buffer, can be no greater
);
let senderNonce = crypt.generateNonce(); let senderNonce = crypt.generateNonce();
sd.send(senderNonce); sd.send(senderNonce);
@@ -213,7 +232,7 @@ class Dial {
r.feed(new reader.Buffer(new Uint8Array(decoded), () => {}), () => {}); r.feed(new reader.Buffer(new Uint8Array(decoded), () => {}), () => {});
} catch (e) { } catch (e) {
r.close(); r.closeWithReason(e);
} }
}); });
@@ -319,10 +338,16 @@ export class Socket {
} }
}); });
streamHandler.serve().catch(() => {});
callbacks.connected(); callbacks.connected();
streamHandler.serve().catch(e => {
if (process.env.NODE_ENV !== "development") {
return;
}
console.trace(e);
});
this.streamHandler = streamHandler; this.streamHandler = streamHandler;
} catch (e) { } catch (e) {
callbacks.failed(e); callbacks.failed(e);

View File

@@ -15,7 +15,7 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
export default class Exception { export default class Exception extends Error {
/** /**
* constructor * constructor
* *
@@ -24,17 +24,8 @@ export default class Exception {
* *
*/ */
constructor(message, temporary) { constructor(message, temporary) {
this.message = message; super(message);
this.temporary = temporary; this.temporary = temporary;
} }
/**
* Return the error string
*
* @returns {string} Error message
*
*/
toString() {
return this.message;
}
} }

View File

@@ -222,12 +222,23 @@ export class Multiple {
* *
*/ */
close() { close() {
return this.closeWithReason("Reader is closed");
}
/**
* close current reading
*
* @param {string} reason Reason
*
*/
closeWithReason(reason) {
if (this.closed) { if (this.closed) {
return; return;
} }
this.closed = true; this.closed = true;
this.subscribe.reject(new Exception("Reader is closed", false)); this.subscribe.reject(new Exception(reason, false));
this.subscribe.disable(reason);
} }
/** /**
@@ -317,17 +328,23 @@ export class Reader {
* *
*/ */
close() { close() {
return this.closeWithReason("Reader is closed");
}
/**
* close current reading
*
* @param {string} reason Reason
*
*/
closeWithReason(reason) {
if (this.closed) { if (this.closed) {
return; return;
} }
this.closed = true; this.closed = true;
this.buffers.reject( this.buffers.reject(new Exception(reason, false));
new Exception( this.buffers.disable(reason);
"Reader is closed, and thus " + "cannot be operated on",
false
)
);
return this.multiple.close(); return this.multiple.close();
} }

View File

@@ -66,11 +66,7 @@ export class Sender {
this.subscribe.reject(new Exception("Sender has been closed", false)); this.subscribe.reject(new Exception("Sender has been closed", false));
try { this.sendingPoc.catch(() => {});
await this.sendingPoc;
} catch (e) {
// Do nothing
}
this.reject(new Exception("Sending has been cancelled", true)); this.reject(new Exception("Sending has been cancelled", true));
} }

View File

@@ -29,6 +29,7 @@ export class Subscribe {
this.res = null; this.res = null;
this.rej = null; this.rej = null;
this.pending = []; this.pending = [];
this.disabled = null;
} }
/** /**
@@ -78,6 +79,10 @@ export class Subscribe {
* *
*/ */
subscribe() { subscribe() {
if (this.disabled) {
throw new Exception(this.disabled, false);
}
if (this.pending.length > 0) { if (this.pending.length > 0) {
let p = this.pending.shift(); let p = this.pending.shift();
@@ -111,4 +116,14 @@ export class Subscribe {
}; };
}); });
} }
/**
* Disable current subscriber when all internal data is readed
*
* @param {string} reason Reason of the disable
*
*/
disable(reason) {
this.disabled = reason;
}
} }