From 20b224c5e0251b4b2a16fda43e009b24cc084730 Mon Sep 17 00:00:00 2001 From: matejka Date: Thu, 15 Nov 2018 19:48:47 +0100 Subject: [PATCH] add error parser for no-feathers endpoint --- README.md | 8 +- lib/index.js | 2 + lib/services/forward/forward.hooks.js | 131 ++++++++++++++++-------- lib/services/forward/forward.service.js | 2 +- package.json | 2 +- 5 files changed, 96 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index c2654ac..bf18295 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,9 @@ Configure [Feathers application](https://feathersjs.com) to forward all unknown __Options:__ - `uri` (**required**) - Uri to remote server -- `reqHeaders` - Headers to forward from REST req object -- `resHeaders` - Headers to be added to REST res object from forwarded response +- `endpoint` - Type of endpoint, for now support `feathers` (not specified === `feathers`) and everything else is default behaviour of request parsing +- `reqHeaders` - Headers to forward from REST req object (case sensitive) +- `resHeaders` - Headers to be added to REST res object from forwarded response (case sensitive) ## Example @@ -57,7 +58,8 @@ app.configure(services); // Forward everything else app.configure(forward({ uri: 'https://example.com', - reqHeaders: ['Authorization'], + endpoint: 'feathers', + reqHeaders: ['authorization'], resHeaders: ['custom-header'] })); diff --git a/lib/index.js b/lib/index.js index 1dcb85f..76950d1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,6 +18,7 @@ function requiredParameterHandler(parameterName) { */ function configure({ uri = requiredParameterHandler('uri'), + endpoint = 'feathers', reqHeaders = [], resHeaders = [] }) { @@ -25,6 +26,7 @@ function configure({ const app = this; app.set('FEATHERS_FORWARD__REMOTE_FORWARDING_URL', uri); + app.set('FEATHERS_FORWARD__ENDPOINT', endpoint); app.set('FEATHERS_FORWARD__REQ_HEADERS', reqHeaders); app.set('FEATHERS_FORWARD__RES_HEADERS', resHeaders); diff --git a/lib/services/forward/forward.hooks.js b/lib/services/forward/forward.hooks.js index d8dcc56..ef214ec 100644 --- a/lib/services/forward/forward.hooks.js +++ b/lib/services/forward/forward.hooks.js @@ -1,59 +1,102 @@ const feathersErrors = require('@feathersjs/errors'); /** - * Forward error + * Parse feathers error * - * @param {object} ctx Context + * @param {object} ctx Hook context + * @param {Error} error Error thrown */ -function forwardError(ctx) { - const {error} = ctx; +const parseFeathersError = function (ctx, error) { + const {message} = error.response.data; - if (error.response) { - const {message} = error.response.data; + const options = error.response.data.data || {}; + options.errors = error.response.data.errors; - const options = error.response.data.data || {}; - options.errors = error.response.data.errors; + if (feathersErrors[error.response.data.name]) { + ctx.error = new feathersErrors[error.response.data.name](message, options); + return; + } - if (feathersErrors[error.response.data.name]) { - ctx.error = new feathersErrors[error.response.data.name](message, options); - return; - } + ctx.error = new feathersErrors.GeneralError(message, options); +}; - ctx.error = new feathersErrors.GeneralError(message, options); +/** + * Parse default error + * + * @param {object} ctx Hook context + * @param {Error} error Error thrown + */ +const parseDefaultError = function (ctx, error) { + const {status, data} = error.response; + + const options = {}; + + if (feathersErrors[status]) { + ctx.error = new feathersErrors[status](data, options); return; } - ctx.error = new feathersErrors.Unavailable(error.message); + ctx.error = new feathersErrors.GeneralError(data, options); +}; + +/** + * Forward error + * + * @param {object} app App + * @return {function} Hook forward function + */ +function forwardError(app) { + const type = app.get('FEATHERS_FORWARD__ENDPOINT'); + + return ctx => { + const {error} = ctx; + + if (error.response) { + switch (type) { + case 'feathers': + parseFeathersError(ctx, error); + break; + default: + parseDefaultError(ctx, error); + break; + } + return; + } + + ctx.error = new feathersErrors.Unavailable(error.message); + }; } -module.exports = { - before: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [] - }, - - after: { - all: [], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [] - }, - - error: { - all: [forwardError], - find: [], - get: [], - create: [], - update: [], - patch: [], - remove: [] - } +module.exports = function (app) { + return { + before: { + all: [], + find: [], + get: [], + create: [], + update: [], + patch: [], + remove: [] + }, + + after: { + all: [], + find: [], + get: [], + create: [], + update: [], + patch: [], + remove: [] + }, + + error: { + all: [forwardError(app)], + find: [], + get: [], + create: [], + update: [], + patch: [], + remove: [] + } + }; }; diff --git a/lib/services/forward/forward.service.js b/lib/services/forward/forward.service.js index eb2d1df..fd1585b 100644 --- a/lib/services/forward/forward.service.js +++ b/lib/services/forward/forward.service.js @@ -114,5 +114,5 @@ module.exports = function (app) { const matchAll = '[^]*'; app.use(matchAll, new ForwardService()); const forward = app.service(matchAll); - forward.hooks(hooks); + forward.hooks(hooks(app)); }; diff --git a/package.json b/package.json index 846ee7d..17faf33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "feathers-forward", - "version": "1.1.1", + "version": "1.2.0", "description": "A Feathers service addapter for forwarding messages to another feathers application", "license": "MIT", "engines": {