From 027f94c867477a0adcab46be1e23dd387a494b97 Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Tue, 8 May 2018 16:46:34 -0700 Subject: [PATCH] Add TimeoutError and throw on p_timeout --- Errors.js | 9 +++++++++ utils.js | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Errors.js b/Errors.js index 461576d..9a8cb83 100644 --- a/Errors.js +++ b/Errors.js @@ -26,6 +26,15 @@ class TransportError extends Error { } errors.TransportError = TransportError; +class TimeoutError extends Error { + constructor(message) { + super(message || "Timed out"); + this.name = "TimeoutError" + } +} +errors.TimeoutError = TimeoutError; + + /*---- Below here are errors copied from previous Dweb-Transport and not currently used */ /* class ObsoleteError extends Error { diff --git a/utils.js b/utils.js index 58be05e..4d5f64a 100644 --- a/utils.js +++ b/utils.js @@ -1,3 +1,4 @@ +const errors = require('./Errors'); utils = {}; //utility functions @@ -26,12 +27,13 @@ utils.p_timeout = function(promise, ms, errorstr) { promise: A promise we want to watch to completion ms: Time in milliseconds to allow it to run errorstr: Error message in reject error + throws: TimeoutError on timeout with message = errorstr */ let timer = null; return Promise.race([ new Promise((resolve, reject) => { - timer = setTimeout(reject, ms, errorstr || `Timed out in ${ms}ms`); + timer = setTimeout(reject, ms, new errors.TimeoutError(errorstr || `Timed out in ${ms}ms`)); }), promise.then((value) => { clearTimeout(timer);