Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve configurability #1654

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions README.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion bin/generate-docs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash -ex
#!/usr/bin/env bash
set -ex

rm -r docs || true

Expand Down
2 changes: 1 addition & 1 deletion bin/start
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
#!/usr/bin/env bash
exec node index.js
2 changes: 1 addition & 1 deletion bin/units
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# run tests with pipefail to avoid false passes
# see https://github.com/pelias/pelias/issues/744
Expand Down
7 changes: 6 additions & 1 deletion middleware/sendJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ const es = require('elasticsearch');
const logger = require( 'pelias-logger' ).get( 'api' );
const PeliasParameterError = require('../sanitizer/PeliasParameterError');
const PeliasTimeoutError = require('../sanitizer/PeliasTimeoutError');
const PeliasServiceError = require('../sanitizer/PeliasServiceError');

function isParameterError(error) {
return error instanceof PeliasParameterError;
}

function isServiceError(error) {
return error instanceof PeliasServiceError;
}

function isTimeoutError(error) {
return error instanceof PeliasTimeoutError ||
error instanceof es.errors.RequestTimeout;
Expand Down Expand Up @@ -36,7 +41,7 @@ function sendJSONResponse(req, res, next) {
const errorCodes = errors.map(function(error) {
if (isParameterError(error)) {
return 400;
} else if (isTimeoutError(error) || isElasticsearchError(error)) {
} else if (isTimeoutError(error) || isElasticsearchError(error) || isServiceError(error)) {
return 502;
} else {
return 500;
Expand Down
6 changes: 6 additions & 0 deletions query/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ query.filter( views.focus_point_filter );

// --------------------------------

const overrides = config.get('api.autocomplete.default_overrides');

/**
map request variables to query variables for all inputs
provided by this HTTP request.
Expand All @@ -75,6 +77,10 @@ function generateQuery( clean ){

const vs = new peliasQuery.Vars( defaults );

if (_.isObject(overrides)) {
vs.set(overrides);
}

// sources
if( _.isArray(clean.sources) && !_.isEmpty(clean.sources) ){
vs.var( 'sources', clean.sources );
Expand Down
7 changes: 7 additions & 0 deletions query/reverse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require('lodash');
const peliasQuery = require('pelias-query');
const config = require('pelias-config').generate();
const defaults = require('./reverse_defaults');

//------------------------------
Expand All @@ -23,10 +24,16 @@ query.filter( peliasQuery.view.boundary_gid );

// --------------------------------

const overrides = config.get('api.reverse.default_overrides');

function generateQuery( clean ){

const vs = new peliasQuery.Vars( defaults );

if (_.isObject(overrides)) {
vs.set(overrides);
}

// set size
if( clean.querySize ){
vs.var( 'size', clean.querySize);
Expand Down
6 changes: 6 additions & 0 deletions query/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require('lodash');
const peliasQuery = require('pelias-query');
const defaults = require('./search_defaults');
const config = require('pelias-config').generate();
const textParser = require('./text_parser');

//------------------------------
Expand All @@ -24,6 +25,8 @@ fallbackQuery.filter( peliasQuery.view.categories );
fallbackQuery.filter( peliasQuery.view.boundary_gid );
// --------------------------------

const overrides = config.get('api.search.default_overrides');

/**
map request variables to query variables for all inputs
provided by this HTTP request.
Expand All @@ -32,6 +35,9 @@ function generateQuery( clean ){

const vs = new peliasQuery.Vars( defaults );

if (_.isObject(overrides)) {
vs.set(overrides);
}

// input text
vs.var( 'input:name', clean.text );
Expand Down
15 changes: 15 additions & 0 deletions sanitizer/PeliasServiceError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PeliasServiceError extends Error {
constructor(message = '') {
super(message);
}

toString() {
return this.message;
}

toJSON() {
return this.message;
}
}

module.exports = PeliasServiceError;
9 changes: 7 additions & 2 deletions sanitizer/sanitizeAll.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const PeliasParameterError = require('./PeliasParameterError');
const PeliasTimeoutError = require('../sanitizer/PeliasTimeoutError');
const PeliasServiceError = require('../sanitizer/PeliasServiceError');

function getCorrectErrorType(message) {
if (message.includes( 'Timeout')) {
if (message.includes( 'Timeout') || message.includes('timeout')) {
return new PeliasTimeoutError(message);
} else if ( message.includes('parse response') ||
message.includes('ECONN') ||
message.includes('ENOTFOUND')) {
return new PeliasServiceError(message);
}

// most errors are parameter errors
Expand All @@ -15,7 +20,7 @@ function getCorrectErrorType(message) {
// on Error objects here
function ensureInstanceOfError(error) {
if (error instanceof Error) {
// preserve the message and stack trace of existing Error objecs
// preserve the message and stack trace of existing Error objects
const newError = getCorrectErrorType(error.message);
newError.stack = error.stack;
return newError;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/helper/diffPlaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ module.exports.tests.normalizeString = function (test, common) {

test('diacritics', function (t) {
t.equal(normalizeString('Malmö'), 'malmo');
t.equal(normalizeString('Grolmanstraße'), 'grolmanstraße');
t.equal(normalizeString('Grolmanstraße'), 'grolmanstrasse');
t.equal(normalizeString('àáâãäåấắæầằçḉèéêëếḗềḕ'), 'aaaaaaaaaeaacceeeeeeee');
t.end();
});
Expand Down
19 changes: 19 additions & 0 deletions test/unit/middleware/sendJSON.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const es = require('elasticsearch');
const middleware = require('../../../middleware/sendJSON');
const PeliasTimeoutError = require('../../../sanitizer/PeliasTimeoutError');
const PeliasServiceError = require('../../../sanitizer/PeliasServiceError');

module.exports.tests = {};

Expand Down Expand Up @@ -243,6 +244,24 @@ module.exports.tests.unknown_exception = function(test, common) {
});
};

module.exports.tests.econnrefused = function(test, common) {
test('connection refused', function(t) {
var res = { body: { geocoding: {
errors: [ new PeliasServiceError('Some service error') ]
}}};

res.status = function( code ){
return { json: function( body ){
t.equal( code, 502, 'Bad Gateway' );
t.deepEqual( body, res.body, 'body set' );
t.end();
}};
};

middleware(null, res);
});
};

module.exports.all = function (tape, common) {

function test(name, testFunction) {
Expand Down
33 changes: 33 additions & 0 deletions test/unit/sanitizer/sanitizeAll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var sanitizeAll = require('../../../sanitizer/sanitizeAll');
const PeliasParameterError = require('../../../sanitizer/PeliasParameterError');
const PeliasTimeoutError = require('../../../sanitizer/PeliasTimeoutError');
const PeliasServiceError = require('../../../sanitizer/PeliasServiceError');

module.exports.tests = {};

Expand Down Expand Up @@ -154,6 +155,38 @@ module.exports.tests.all = function(test, common) {
t.end();
});

test('Various connection errors should be converted to a PeliasServiceError type', function(t) {
var req = {};
const error_messages = [
'connect ECONNREFUSED 127.0.0.1:12345',
'getaddrinfo ENOTFOUND foobar',
'http://127.0.0.1:12345/parse?address=3400%20Kane%20Hill%20Rd could not parse response: foobar',
];
var sanitizers = {
'first': {
sanitize: function(){
req.clean.a = 'first sanitizer';
return {
errors: error_messages.map(msg => new Error(msg)),
warnings: []
};
}
}
};

var expected_req = {
clean: {
a: 'first sanitizer'
},
errors: error_messages.map(msg => new PeliasServiceError(msg)),
warnings: []
};

sanitizeAll.runAllChecks(req, sanitizers);
t.deepEquals(req, expected_req);
t.end();
});

test('req.query should be passed to individual sanitizers when available', function(t) {
var req = {
query: {
Expand Down