-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from touch4it/endpoint-type
add error parser for no-feathers endpoint
- Loading branch information
Showing
5 changed files
with
96 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: [] | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters