2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-07 18:07:38 +08:00
|
|
|
const fs = require('fs');
|
2019-10-23 00:22:33 +08:00
|
|
|
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);
|
2017-11-15 22:07:30 +08:00
|
|
|
}
|
2018-03-23 20:30:47 +08:00
|
|
|
|
|
|
|
function isRateLimitError (err) {
|
|
|
|
return err.type === 'limit' && err.subtype === 'rate-limit';
|
|
|
|
}
|