cleanup error responses
Co-Authored-By: Andrew Calcutt <acalcutt@techidiots.net>
This commit is contained in:
parent
d98cc3328f
commit
4c6f379f4e
2 changed files with 49 additions and 36 deletions
|
@ -64,42 +64,43 @@ export const serve_style = {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get(`/:id/:sprite{/:spriteID}{@:scale}{.:format}`, (req, res, next) => {
|
app.get(`/:id/:sprite{/:spriteID}{@:scale}{.:format}`, (req, res, next) => {
|
||||||
console.log(req.params);
|
|
||||||
const { spriteID = 'default', id, format } = req.params;
|
const { spriteID = 'default', id, format } = req.params;
|
||||||
const scale = allowedSpriteScales(req.params.scale);
|
const scale = allowedSpriteScales(req.params.scale);
|
||||||
try {
|
|
||||||
if (
|
if (
|
||||||
!allowedSpriteFormats(format) ||
|
!allowedSpriteFormats(format) ||
|
||||||
((id == 256 || id == 512) && format === 'json')
|
((id == 256 || id == 512) && format === 'json')
|
||||||
) {
|
) {
|
||||||
//Workaround for {/:tileSize}/:id.json' and /styles/:id/wmts.xml
|
//Workaround for {/:tileSize}/:id.json' and /styles/:id/wmts.xml
|
||||||
next('route');
|
return next('route');
|
||||||
} else {
|
|
||||||
const item = repo[id];
|
|
||||||
const sprite = item.spritePaths.find(
|
|
||||||
(sprite) => sprite.id === spriteID,
|
|
||||||
);
|
|
||||||
if (sprite) {
|
|
||||||
const filename = `${sprite.path + scale}.${format}`;
|
|
||||||
return fs.readFile(filename, (err, data) => {
|
|
||||||
if (err) {
|
|
||||||
console.log('Sprite load error:', filename);
|
|
||||||
return res.sendStatus(404);
|
|
||||||
} else {
|
|
||||||
if (format === 'json')
|
|
||||||
res.header('Content-type', 'application/json');
|
|
||||||
if (format === 'png') res.header('Content-type', 'image/png');
|
|
||||||
return res.send(data);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} else {
|
const item = repo[id];
|
||||||
|
if (!item) {
|
||||||
|
return res.sendStatus(404); // Ensure item exists first to prevent errors
|
||||||
|
}
|
||||||
|
|
||||||
|
const sprite = item.spritePaths.find((sprite) => sprite.id === spriteID);
|
||||||
|
if (!sprite) {
|
||||||
return res.status(400).send('Bad Sprite ID or Scale');
|
return res.status(400).send('Bad Sprite ID or Scale');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const spriteScale = allowedSpriteScales(scale);
|
||||||
|
const filename = `${sprite.path}${spriteScale}.${format}`;
|
||||||
|
|
||||||
|
fs.readFile(filename, (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
console.error('Sprite load error: %s, Error: %s', filename, err);
|
||||||
|
return res.sendStatus(404);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
if (format === 'json') {
|
||||||
next('route');
|
res.header('Content-type', 'application/json');
|
||||||
|
} else if (format === 'png') {
|
||||||
|
res.header('Content-type', 'image/png');
|
||||||
}
|
}
|
||||||
|
return res.send(data);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
|
|
16
src/utils.js
16
src/utils.js
|
@ -196,14 +196,23 @@ export function fixTileJSONCenter(tileJSON) {
|
||||||
function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) {
|
function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
|
||||||
|
if (!name || typeof name !== 'string' || name.trim() === '') {
|
||||||
|
console.error('ERROR: Invalid font name: %s', name);
|
||||||
|
return reject('Invalid font name');
|
||||||
|
}
|
||||||
|
if (!/^\d+-\d+$/.test(range)) {
|
||||||
|
console.error('ERROR: Invalid range: %s', range);
|
||||||
|
return reject('Invalid range');
|
||||||
|
}
|
||||||
const filename = path.join(fontPath, name, `${range}.pbf`);
|
const filename = path.join(fontPath, name, `${range}.pbf`);
|
||||||
if (!fallbacks) {
|
if (!fallbacks) {
|
||||||
fallbacks = clone(allowedFonts || {});
|
fallbacks = clone(allowedFonts || {});
|
||||||
}
|
}
|
||||||
delete fallbacks[name];
|
delete fallbacks[name];
|
||||||
|
// eslint-disable-next-line security/detect-non-literal-fs-filename
|
||||||
fs.readFile(filename, (err, data) => {
|
fs.readFile(filename, (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(`ERROR: Font not found: ${name}`);
|
console.error('ERROR: Font not found: %s, Error: %s', filename, err);
|
||||||
if (fallbacks && Object.keys(fallbacks).length) {
|
if (fallbacks && Object.keys(fallbacks).length) {
|
||||||
let fallbackName;
|
let fallbackName;
|
||||||
|
|
||||||
|
@ -219,7 +228,10 @@ function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error(`ERROR: Trying to use ${fallbackName} as a fallback`);
|
console.error(
|
||||||
|
`ERROR: Trying to use %s as a fallback`,
|
||||||
|
fallbackName,
|
||||||
|
);
|
||||||
delete fallbacks[fallbackName];
|
delete fallbacks[fallbackName];
|
||||||
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(
|
getFontPbf(null, fontPath, fallbackName, range, fallbacks).then(
|
||||||
resolve,
|
resolve,
|
||||||
|
|
Loading…
Reference in a new issue