Browse Source

allocate new stacktrace for debugging (#44)

* allocate new stacktrace for debugging

* move errors into functions
session-estimator
Jake Verbaten 2 years ago
committed by GitHub
parent
commit
6b8ba2854e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      lib/errors.js
  2. 13
      lib/io.js

14
lib/errors.js

@ -1,8 +1,14 @@
exports.UNKNOWN_COMMAND = 1
exports.INVALID_TOKEN = 2
exports.TIMEOUT = new Error('Request timed out')
exports.TIMEOUT.code = 'ETIMEDOUT'
exports.createTimeoutError = () => {
const timeoutErr = new Error('Request timed out')
timeoutErr.code = 'ETIMEDOUT'
return timeoutErr
}
exports.DESTROY = new Error('Request destroyed')
exports.DESTROY.code = 'EDESTROYED'
exports.createDestroyedError = () => {
const destroyErr = new Error('Request destroyed')
destroyErr.code = 'EDESTROYED'
return destroyErr
}

13
lib/io.js

@ -4,7 +4,7 @@ const c = require('compact-encoding')
const bind = require('bind-easy')
const b4a = require('b4a')
const peer = require('./peer')
const { INVALID_TOKEN, TIMEOUT, DESTROY } = require('./errors')
const errors = require('./errors')
const VERSION = 0b11
const RESPONSE_ID = (0b0001 << 4) | VERSION
@ -48,7 +48,7 @@ module.exports = class IO {
const req = Request.decode(this, socket, from, state)
if (req === null) return
if (req.token !== null && !b4a.equals(req.token, this.token(req.from, 1)) && !b4a.equals(req.token, this.token(req.from, 0))) {
req.error(INVALID_TOKEN, { token: true })
req.error(errors.INVALID_TOKEN, { token: true })
return
}
this.onrequest(req, external)
@ -66,7 +66,7 @@ module.exports = class IO {
if (i === this.inflight.length - 1) this.inflight.pop()
else this.inflight[i] = this.inflight.pop()
// TODO: Auto retry here if INVALID_TOKEN is returned?
// TODO: Auto retry here if errors.INVALID_TOKEN is returned?
if (req._timeout) {
clearTimeout(req._timeout)
@ -110,7 +110,8 @@ module.exports = class IO {
if (req._timeout) clearTimeout(req._timeout)
req._timeout = null
req.destroyed = true
req.onerror(DESTROY, req)
req.onerror(errors.createDestroyedError(), req)
}
this._destroying = new Promise((resolve) => {
@ -294,7 +295,7 @@ class Request {
if (i === this._io.inflight.length - 1) this._io.inflight.pop()
else this._io.inflight[i] = this._io.inflight.pop()
this.onerror(err || DESTROY, this)
this.onerror(err || errors.createDestroyedError(), this)
}
_sendReply (error, value, token, hasCloserNodes, from, socket, onflush) {
@ -394,7 +395,7 @@ function oncycle (req) {
req._timeout = null
req.oncycle(req)
if (req.sent >= req.retries) {
req.destroy(TIMEOUT)
req.destroy(errors.createTimeoutError())
req._io.ontimeout(req)
} else {
req.send()

Loading…
Cancel
Save