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
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export default class Exception {
export default class Exception extends Error {
/**
* constructor
*
@@ -24,17 +24,8 @@ export default class Exception {
*
*/
constructor(message, temporary) {
this.message = message;
super(message);
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() {
return this.closeWithReason("Reader is closed");
}
/**
* close current reading
*
* @param {string} reason Reason
*
*/
closeWithReason(reason) {
if (this.closed) {
return;
}
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() {
return this.closeWithReason("Reader is closed");
}
/**
* close current reading
*
* @param {string} reason Reason
*
*/
closeWithReason(reason) {
if (this.closed) {
return;
}
this.closed = true;
this.buffers.reject(
new Exception(
"Reader is closed, and thus " + "cannot be operated on",
false
)
);
this.buffers.reject(new Exception(reason, false));
this.buffers.disable(reason);
return this.multiple.close();
}

View File

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

View File

@@ -29,6 +29,7 @@ export class Subscribe {
this.res = null;
this.rej = null;
this.pending = [];
this.disabled = null;
}
/**
@@ -78,6 +79,10 @@ export class Subscribe {
*
*/
subscribe() {
if (this.disabled) {
throw new Exception(this.disabled, false);
}
if (this.pending.length > 0) {
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;
}
}