framework icon indicating copy to clipboard operation
framework copied to clipboard

How to custom response 408 timeout on spesific route?

Open aalfiann opened this issue 2 years ago • 8 comments

Hi sir,

I used this to handle the response timeout in totaljs 3,

ROUTE('#408', function () {
    var self = this;
    self.json({ code: 408, message: 'Error timeout!'});
});

That will return same timeout response for all routes and that's OK.

Right now, I want to custom the response timeout only to spesific route, but the other routes still using default timeout response like above?

Is this possible? How to do this in TotalJS 3?

Thanks for your help.

aalfiann avatar Sep 15 '23 10:09 aalfiann

@aalfiann Each route can have its own timeout. But 409 means conflict, not a timeout. However, in this route you create a custom response based on a URL address, e.g. self.url or self.uri {URI object}.

petersirka avatar Sep 15 '23 10:09 petersirka

sorry, I mean 408 timeout.

Yes, I was thinking to do like that, but I couldn't determine if the status is 408 and return the custom response timeout.

aalfiann avatar Sep 15 '23 10:09 aalfiann

Here is my controller

exports.install = function () {
  ROUTE('/test', viewIndex)
  ROUTE('/test/contact', viewContact)
  ROUTE('/test/timeout', testTimeout, ['get', 5000]) // this request will timeout for 5 seconds
}

function viewIndex () {
  const self = this
  self.view('index')
}

function viewContact () {
  const self = this
  self.view('contact')
}

function _blockingTest (ms) {
  ms = (ms === undefined ? 1000 : ms)
  const start = Date.now()
  const time = start + ms
  while (Date.now() < time) {
    // empty progress
  }
  return start
}

function testTimeout () {
  const self = this
  // try to blocking request for 10 seconds so this will get timout
  _blockingTest(10000) 
  self.view('index')
}

I found that timeout is not working?

aalfiann avatar Sep 15 '23 11:09 aalfiann

Your _blockingTest function is blocking entire application and the framework can't even process any timeouts. Try to use bellow code instead:

function testTimeout () {
  setTimeout(() => this.view('index'), 10000);
}

molda avatar Sep 15 '23 11:09 molda

@molda , thanks the timeout working well.

now, how to handle the timeout to using the custom timeout response?

function testTimeout () {
  // try to blocking request for 10 seconds so this will get timout
  setTimeout(() => this.view('index'), 10000)
  
  // if the timeout happened, set custom timeout response
  // code below here not work, it giving response directly
  // var self = this;
  // self.json({ code: 408, message: 'This is Custom Error timeout!'});
}

aalfiann avatar Sep 15 '23 11:09 aalfiann

Thanks, I solved this by put condition on route default

  ROUTE('#408', function () {
    const self = this
    if (self.req.uri.path === '/test/timeout') {
      self.json({ code: 408, message: 'This is custom error timeout!' })
    } else {
      self.json({ code: 408, message: 'Error timeout!' })
    }
  })

aalfiann avatar Sep 15 '23 12:09 aalfiann

found new issue, why the status return 200 ?

when I set self.status = 408 it cause the browser looping.

aalfiann avatar Sep 15 '23 13:09 aalfiann

In your code in ROUTE('#408', you still need to set the status. So before self.json(.. add self.status = 408;

molda avatar Sep 15 '23 14:09 molda