Skip to content

Commit

Permalink
chore: consistent error messages, hide stacktrace in HTTP response
Browse files Browse the repository at this point in the history
Signed-off-by: Martin d'Allens <martin.dallens@liberty-rider.com>
  • Loading branch information
Caerbannog committed Nov 26, 2023
1 parent 40ade06 commit d0e4c92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const precisePx = (ll, zoom) => {
* @param {number} z Map zoom level.
*/
const drawMarker = (ctx, marker, z) => {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const img = new Image();
const pixelCoords = precisePx(marker.location, z);

Expand Down Expand Up @@ -79,7 +79,7 @@ const drawMarker = (ctx, marker, z) => {

img.onload = drawOnCanvas;
img.onerror = (err) => {
throw err;
reject(err);
};
img.src = marker.icon;
});
Expand Down
6 changes: 5 additions & 1 deletion src/serve_font.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export const serve_font = async (options, allowedFonts) => {
res.header('Last-Modified', lastModified);
return res.send(concatenated);
} catch (err) {
res.status(400).header('Content-Type', 'text/plain').send(err);
console.error('ERROR: font serving failed', err);
res
.status(400)
.header('Content-Type', 'text/plain')
.send('Font serving failed');
}
},
);
Expand Down
25 changes: 18 additions & 7 deletions src/serve_rendered.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ const respondImage = (
renderer.render(params, (err, data) => {
pool.release(renderer);
if (err) {
console.error(err);
return res.status(500).header('Content-Type', 'text/plain').send(err);
console.error('ERROR: render failed', err);
return res
.status(500)
.header('Content-Type', 'text/plain')
.send('Render failed');
}

const image = sharp(data, {
Expand Down Expand Up @@ -898,8 +901,9 @@ export const serve_rendered = {
let data = tileinfo.data;
let headers = tileinfo.header;
if (data == undefined) {
if (options.verbose)
console.log('MBTiles error, serving empty', err);
if (options.verbose) {
console.log('PMTiles error, serving empty', err);
}
createEmptyResponse(
sourceInfo.format,
sourceInfo.color,
Expand Down Expand Up @@ -931,8 +935,9 @@ export const serve_rendered = {
} else if (sourceType === 'mbtiles') {
source.getTile(z, x, y, (err, data, headers) => {
if (err) {
if (options.verbose)
if (options.verbose) {
console.log('MBTiles error, serving empty', err);
}
createEmptyResponse(
sourceInfo.format,
sourceInfo.color,
Expand Down Expand Up @@ -1001,6 +1006,12 @@ export const serve_rendered = {
parsedResponse.data = responseData;
callback(null, parsedResponse);
} catch (error) {
if (options.verbose) {
console.log(
`Resource error for ${req.url}, serving empty`,
err,
);
}
const parts = url.parse(req.url);
const extension = path.extname(parts.pathname).toLowerCase();
const format = extensionToFormat[extension] || '';
Expand All @@ -1027,7 +1038,7 @@ export const serve_rendered = {
try {
styleJSON = JSON.parse(fs.readFileSync(styleJSONPath));
} catch (e) {
console.log('Error parsing style file');
console.error('ERROR: Error parsing style file');
return false;
}

Expand Down Expand Up @@ -1171,7 +1182,7 @@ export const serve_rendered = {
map.sources[name] = new MBTiles(inputFile + '?mode=ro', (err) => {
map.sources[name].getInfo((err, info) => {
if (err) {
console.error(err);
console.error('ERROR: getInfo failed', err);
return;
}
map.sourceTypes[name] = 'mbtiles';
Expand Down

0 comments on commit d0e4c92

Please sign in to comment.