Windshaft-cartodb/lib/api/middlewares/vector-error.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

'use strict';
2017-11-07 18:07:38 +08:00
const fs = require('fs');
const path = require('path');
const timeoutErrorVectorTile = fs.readFileSync(path.join(__dirname, '/../../../assets/render-timeout-fallback.mvt'));
2017-11-07 18:07:38 +08:00
2019-10-22 01:07:24 +08:00
module.exports = function vectorError () {
return function vectorErrorMiddleware (err, req, res, next) {
if (req.params.format === 'mvt') {
2018-03-23 20:30:47 +08:00
if (isTimeoutError(err) || isRateLimitError(err)) {
2017-11-07 18:07:38 +08:00
res.set('Content-Type', 'application/x-protobuf');
return res.status(429).send(timeoutErrorVectorTile);
}
}
next(err);
2017-11-07 18:17:32 +08:00
};
};
2017-11-07 18:07:38 +08:00
function isRenderTimeoutError (err) {
return err.message === 'Render timed out';
}
function isDatasourceTimeoutError (err) {
return err.message && err.message.match(/canceling statement due to statement timeout/i);
}
function isTimeoutError (err) {
return isRenderTimeoutError(err) || isDatasourceTimeoutError(err);
}
2018-03-23 20:30:47 +08:00
function isRateLimitError (err) {
return err.type === 'limit' && err.subtype === 'rate-limit';
}